Skip to content

Write your first script

This tutorial writes a C# script, attaches it to a GameObject, and turns it on in play mode. It uses only the API that ships in Kolpa.Engine.

  1. In your project root, open bindings/Kolpa.Demo/.
  2. Copy NewBehaviour.cs.template and save it as Spinner.cs.
  3. Rename the class to match the file: public class Spinner : MonoBehaviour.

Your file should start like this:

using Kolpa.Engine;
namespace Kolpa.Demo;
public class Spinner : MonoBehaviour
{
public float DegreesPerSecond = 90f;
public override void OnUpdate(float deltaTime)
{
transform.Rotate(0f, DegreesPerSecond * deltaTime, 0f);
}
}

OnUpdate runs every frame. deltaTime is the frame time in seconds, so the rotation speed does not depend on frame rate.

Build the project so Kolpa.Demo.dll includes the new class. See the engine README.md for the build command.

  1. In the editor, select the GameObject you want to spin.
  2. Add Component -> Script.
  3. In the Behaviour field, type Kolpa.Demo.Spinner, Kolpa.Demo.
  4. File -> Save Scene (Ctrl+S).

The type name is assembly-qualified: Namespace.Class, AssemblyName.

  1. Press Play.
  2. The object spins at the default 90 degrees per second.
  3. Stop play.

Open Spinner.cs and change DegreesPerSecond to 180f. Rebuild, then use Debug -> Reload Assets to load the new assembly without restarting. Play again and the object spins faster.

Note: Hot reload is unavailable in a NativeAOT release build. Scripts there are compiled into the binary.

Previous: Scene settings and sky: scene settings.

Next: Add physics: a dynamic body and a push.