Unity - Game Objects
Game objects are the entities in your game. Everything in you game scene is a game object.
Game objects have components that add features to them, like transform, rigid body and colliders. They can also have children, attached via their transform object
While scripting, you can reference the game object on which the script is attached using the gameObject property.
Creating game objects
During creation you need to specify a name for your new game object:
var myGameObject : GameObject = new GameObject("MyGameObject");
A Transform component is always added to a new game object by default as it is required to specify its position, scale and rotation in the game world.
You can add additional components to the game object using AddComponent, specifying the name of the component:
Each object has a tag that you can use for multiple purposes. You can specify your object tag in this way:
myGameObject.tag = "EnvironmentObject";
Just make sure you added the tag to the Unity tag manager.
Instantiating game objects in the scene
To instantiate a new game object at runtime we'll use the static Instantiate function.
You can use it by passing an existing game object, that will be cloned:
var newObject : GameObject = Instantiate(myGameObject);
You can also specify the desired position and rotation of your new object:
var newObject : GameObject = Instantiate(myGameObject, myPosition, myRotation);
The position will be a Vector3, while the rotation is a Quaternion.
Just note that Instantiate will clone the entire game object hierarchy, with all children cloned as well. Also, all children game objects are activated.
Destroying game objects
You can use the static Destroy function to destroy game object or components.
You can pass a game object to it:
All components and children attached to myGameObject will be destroyed as well.
Destroying components works in the same way. Let's say we want to destroy myGameObject's rigidbody:
We can also destroy the game object on which the script is attached:
Or remove the script component from the game object:
The Destroy function has an optional parameter that lets us destroy the object with a delay.
The game object will be destroyed after 4 seconds.
Retrieving game objects during the game
During the game you may need to retrieve game objects in the scene. There are many different ways to do it, depending on which info you have about the game object.
You can retrieve them by their name:
var myGO : GameObject = GameObject.Find("MyGameObject");
It will be null if no game object with the specified name is available in the scene.
You can also use tag to retrieve active game objects. FindWithTag returns the first game object with the specified tag, null if no objects with that tag exists:
var myGO : GameObject = GameObject.FindWithTag("Environment");
If you need all the game objects with the specified tag use FindGameObjectsWithTag instead. It returns a list of game objects, or null if no game objects with that tag are found:
var objects : GameObject = GameObject.FindGameObjectsWithTag("Environment");