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.
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.
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);
}
}
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.
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
.
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);
}
}
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.
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!