Tutorials / Getting started

✔ OfficialBeginner⏱ 25 minTallow 0.3

Your first script: a spinning pickup

Difficulty: Beginner. Time: about 25 minutes. You need: Tallow 0.3.

In this tutorial you write your first C# script: a gold cube that spins, bobs up and down, and disappears when the player walks into it. Along the way you use the two callbacks every script starts with, public fields that become settings in the Inspector, a little maths and a check.

What you’ll learn: how a script is a component, how Start and Update work, how public fields become settings you can change per object, and how to check a condition every frame.

Step 1: Start a project

  1. In Tallow Manager or the launcher, create a new project from the Empty template and open it.
  2. The editor opens the first scene with a floor, a sun, a camera and a Player.

Press Play and try the player: click the Game view to send it input, W A S D to walk and Esc to get the mouse back. Press Play again to stop. The player is a C# script too, src/Scripts/FirstPersonCharacter.cs, if you want to read one.

Step 2: Make the pickup object

  1. Click the Scene tab. From the menu bar choose GameObject > 3D Object > Cube.
  2. Press F2 in the Hierarchy and rename it Pickup.
  3. In the Inspector, set its Position to 0, 1, 0 and its Scale to 0.5, 0.5, 0.5.
  4. Under Mesh Renderer, set Color to gold (1, 0.78, 0.15), Metallic to 0.6 and Roughness to 0.35.

The gold Pickup cube selected in the Scene view, with its Mesh Renderer in the Inspector.
The Pickup cube, in front of where the player starts.

Step 3: Create the script

  1. In the Project panel, open the Scripts folder.
  2. Click + and choose C# Script (code) > Basic Component. Name it Pickup. It opens in the Scripts tab.

A new script has the two callbacks every gameplay class starts with:

using System.Numerics;
using Tallow;

namespace MyFirstGame;

[Component]
public class Pickup : Script
{
    public float Speed = 1.0f;

    public override void Start()
    {
    }

    public override void Update(TickInput input, float deltaTime)
    {
    }
}

How it is put together:

  • public class Pickup : Script is the component. [Component] is what lets the editor list it under Add Component > Scripts.
  • Public fields are the component’s settings: they show in the Inspector and are saved with the scene, per object.
  • Start runs once, when the game starts.
  • Update runs every frame, with the player’s controls for that frame (input) and the seconds since the last one (deltaTime).

Replace the file’s contents with a script that spins the pickup 90 degrees a second:

using System.Numerics;
using Tallow;

namespace MyFirstGame;

[Component]
public class Pickup : Script
{
    public float SpinSpeed = 90;

    public override void Update(TickInput input, float deltaTime)
    {
        Transform.Rotate(new Vector3(0, SpinSpeed * deltaTime, 0));
    }
}

Save with Ctrl+S. The editor compiles the project in the background; the Console shows the result, and any error is listed there and marked next to the line.

Step 4: Put it on the cube

  1. Select the Pickup object in the Hierarchy. At the bottom of the Inspector, click Add Component, open Scripts and pick Pickup (or type Pickup in the search box).
  2. Press Play. The cube spins. Press Play again to stop.

A component like this is no different from a Light or a Collider: put it on as many objects as you like, and each one runs its own copy.

Step 5: Make the speed a setting

SpinSpeed is a public field, so the Inspector shows it as Spin Speed. Select the Pickup and try 180 or 30: the value is saved with the scene and the script reads it, with no recompile. A field can be set per object, so a slow pickup and a fast one use the same script.

Step 6: Bob up and down

Now the cube floats up and down around where you placed it. The script remembers its starting position and adds a sine wave to it. Add the fields, fill in Start, and change Update:

using System.Numerics;
using Tallow;

namespace MyFirstGame;

[Component]
public class Pickup : Script
{
    public float SpinSpeed = 90;
    public float BobHeight = 0.25f;
    public float BobSpeed = 2;

    private Vector3 _startPosition;
    private float _time;

    public override void Start()
    {
        _startPosition = Transform.Position;
    }

    public override void Update(TickInput input, float deltaTime)
    {
        _time += deltaTime;
        Transform.Rotate(new Vector3(0, SpinSpeed * deltaTime, 0));
        Transform.Position = _startPosition + new Vector3(0, MathF.Sin(_time * BobSpeed) * BobHeight, 0);
    }
}

Private fields such as _time and _startPosition are the script’s own state: they are not settings, so they stay out of the Inspector and out of the scene file. MathF is .NET’s maths library; Sin gives a smooth wave between −1 and 1.

Save and press Play. The cube spins and floats. Select it and change Bob Height or Spin Speed in the Inspector to see the fields at work.

Step 7: Collect it

The last part checks every frame how far away the player is. When the player is close, it prints a message and removes the pickup. Add the Player field and the check:

using System.Numerics;
using Tallow;

namespace MyFirstGame;

[Component]
public class Pickup : Script
{
    public float SpinSpeed = 90;
    public float BobHeight = 0.25f;
    public float BobSpeed = 2;
    public float PickupRadius = 1.5f;
    public GameObject? Player;

    private Vector3 _startPosition;
    private float _time;

    public override void Start()
    {
        _startPosition = Transform.Position;
        if (Player is null)
            Player = Scene.Find("Player");
    }

    public override void Update(TickInput input, float deltaTime)
    {
        _time += deltaTime;
        Transform.Rotate(new Vector3(0, SpinSpeed * deltaTime, 0));
        Transform.Position = _startPosition + new Vector3(0, MathF.Sin(_time * BobSpeed) * BobHeight, 0);

        if (Player is not { IsValid: true } player)
            return;
        if (Vector3.Distance(player.Transform.Position, Transform.Position) < PickupRadius)
        {
            Log.Info("Picked up!");
            GameObject.Destroy();
        }
    }
}
  • GameObject? Player is a reference to another object. The Inspector gives it an object slot, so you can also drag the Player from the Hierarchy onto it; until then, Start finds the object named Player.
  • is not { IsValid: true } is how you ask whether that object is still there. An object that was destroyed reads as not valid, so the script leaves it alone instead of failing.
  • Log.Info writes to the Console. GameObject.Destroy() removes the pickup at once; its OnDestroy would run at the end of the frame.

Step 8: Tidy up and play

  1. Select the Pickup in the Hierarchy and press Ctrl+D twice. Move the copies to different places, for example 2.5, 1, 2 and -2.5, 1, 3.5.
  2. Give one a bigger Pickup Radius, or a slower Spin Speed.
  3. Press Play and walk into them with WASD. Each one disappears, and the Console shows Picked up! each time.

Playing: three gold pickups floating above the ground in front of the player.
Three pickups, each running its own copy of the script.

Step 9: Where next

Make Picked up! count a score. Give a script on the Player a public int Score and a method AddScore(); the pickup finds that script with Scene.Find("Player")?.GetComponent<ScoreKeeper>() and calls it before it is destroyed. Draw the number in the Player script’s OnGUI with GUI.Label(20, 20, $"Score: {Score}"); OnGUI runs every frame on this machine only.

  • Waits: await NextFrame(); and await Wait(2f); carry on later on the game thread, and end if the script is destroyed (Destroyed).
  • Collisions: OnCollisionEnter, OnCollisionStay and OnCollisionExit report what this object hits, and OnTriggerEnter/Stay/Exit what enters its trigger.
  • Multiplayer: mark a field [Sync] to follow the server, and add [Rpc] methods for calls between machines. The same script runs predicted on the machine that controls the object.
  • Read the C# gameplay guide for the whole API, and Build and share your game to send yours to someone.