๐Ÿ  Home

๐Ÿš€ Unity API Methods

What are Unity API methods?
These are ready-made โ€œcommandsโ€ from Unity that let you interact with game objects: create them, move them, enable or remove them. Instead of writing everything from scratch, you just call the method you need โ€” like a magic word that triggers an action.

In the previous lesson we learned how to access an objectโ€™s components. Now weโ€™ll take it a step further โ€” weโ€™ll start triggering actions on objects: creating, moving, enabling, and destroying them.

๐Ÿงฑ Creating objects: Instantiate()

The Instantiate() method allows you to create copies of pre-prepared objects โ€” prefabs. This is the foundation for spawning bullets, enemies, coins, and any dynamic elements.

A prefab is a template object. You create it once, and then reuse it as many times as you want.


using UnityEngine;

public class Spawner : MonoBehaviour
{
    // Variable to store the prefab
    public GameObject cubePrefab;

    // Update is called once per frame
    void Update()
    {
        // If the space key is pressed
        if (Input.GetKeyDown(KeyCode.Space))
        {
            // Create a new object from the prefab
            Instantiate(cubePrefab, new Vector3(0, 1, 0), Quaternion.identity);
        }
    }
}

๐Ÿ’ก More about prefabs will be covered in separate tutorials โ€” there youโ€™ll learn how to create, configure, and use them in real projects.

๐Ÿšถ Moving objects: transform.Translate()

To move an object in space, you can use the Translate() method. It shifts the object by a given distance.

Time.deltaTime helps make the movement smooth and independent of the frame rate.


using UnityEngine;

public class Mover : MonoBehaviour
{
    // Movement speed
    public float speed = 3f;

    // Update is called once per frame
    void Update()
    {
        // Move the object to the right at 'speed'
        transform.Translate(Vector3.right * speed * Time.deltaTime);
    }
}

๐ŸŽฎ Player control: moving left and right

Now letโ€™s add the most important thing โ€” control. Weโ€™ll move the object left and right using A/D keys or โ† โ†’ arrows. Translate() works perfectly for this.


using UnityEngine;

public class PlayerController : MonoBehaviour
{
    // Movement speed
    public float speed = 5f;

    // Update is called once per frame
    void Update()
    {
        // Get direction from keys (A/D or arrows)
        float input = Input.GetAxis("Horizontal");

        // Move the object along the X axis
        transform.Translate(Vector3.right * input * speed * Time.deltaTime);
    }
}

๐Ÿ’ก Input.GetAxis("Horizontal") returns -1 if left is pressed (A or โ†), +1 if right is pressed (D or โ†’), and 0 if nothing is pressed.

โšก Reacting to 2D triggers

Sometimes you need an object to do something when it collides โ€” collect a coin, activate a portal, etc. Unity provides the OnTriggerEnter2D method for this.

Itโ€™s called automatically if the object has a collider with Is Trigger enabled, and it collides with another object that has a Rigidbody2D.


using UnityEngine;

public class TriggerExample : MonoBehaviour
{
    // This method is called when the object enters a trigger
    void OnTriggerEnter2D(Collider2D other)
    {
        // Print the name of the object we collided with
        Debug.Log("Entered trigger with: " + other.name);
    }
}

๐Ÿ’ก Make sure this object has a BoxCollider2D or another collider with Is Trigger enabled, and the other object has a Rigidbody2D.

๐Ÿ‘ Hiding objects: SetActive()

If you need to temporarily remove an object from the scene without destroying it โ€” use SetActive(false).


using UnityEngine;

public class Hider : MonoBehaviour
{
    // Start is called once when the object is created
    void Start()
    {
        // Disable the object after 3 seconds
        Invoke("Disable", 3f);
    }

    // Method that disables the object
    void Disable()
    {
        gameObject.SetActive(false);
    }
}

๐Ÿ’ฅ Destroying objects: Destroy()

To completely remove an object from the scene, use Destroy(). This is useful for temporary objects: bullets, effects, enemies.


using UnityEngine;

public class SelfDestruct : MonoBehaviour
{
    // Start is called once when the object is created
    void Start()
    {
        // Destroy the object after 5 seconds
        Destroy(gameObject, 5f);
    }
}

๐Ÿ’ก You can call Destroy() without a second parameter to remove the object immediately.

๐Ÿ“Œ Summary

Today we looked at some basic Unity API methods โ€” they help you create objects, move them, hide them, and remove them. These actions appear in almost every game.

But thatโ€™s just the introduction. Unity offers dozens of other methods, and weโ€™ll go over them gradually โ€” in thematic tutorials where theyโ€™re actually needed.

This means youโ€™re ready to move from basics to creating real game mechanics. See you in the first project!