Common Questions Asked by New Unity Developers

Below you can find a list of common questions often asked by new Unity Developers. This list will be updated periodically.


How to Destroy a GameObject?

When using C# you can destroy a GameObject through the use of the UnityEngine.Object.Destroy(…) method. When using this method you have to pass in the reference to the GameObject. An example:

  1. GameObject yourGameObject = new GameObject();
  2. Destroy(yourGameObject);

How to Destroy a Component?

Similar to destroying a GameObject, you can use C# to destroy a component attached to a GameObject. However, be sure to pass in the actual reference of the component. In the below example we remove a Collider component from the GameObject:

  1. Collider collider = yourGameObject.GetComponent<Collider>();
  2. Destroy(collider);

How to Clone or Copy a GameObject?

Through C# you can create a clone or a copy of a GameObject using the UnityEngine.Object.Instantiate(…) method. When using this method you have to pass in the original game object as a parameter. An example:

  1. GameObject yourClonedGameObject = Instantiate(originalGameObject);

How to Change the Parent of a GameObject?

When using C# the parent of a GameObject can be changed at any time by using its Transform component. Any GameObject will have Transform component by default. Please note that you have to assign the actual Transform component of the parent, not just the reference to the parent GameObject itself. An example:

  1. GameObject yourGameObject = new GameObject();
  2. yourGameObject.transform.parent = parentGameObject.tranform;

How to Destroy All Child GameObjects?

Another common question by new Unity developers is how to destroy all child objects using the following code. This code will iterate over all children under the GameObject and delete them one at a time. Please, also have a look at a Method Extension available that was created for this exact purpose.

  1. foreach(Transform transform in yourGameObject.transform)
  2. {
  3. Destroy(transform.gameObject);
  4. }

How to Move a GameObject?

in order to move a GameObject, you have modify the position property of the UnityEngine.Transform component. Simply try ‘adding’ a UnityEngine.Vector3 to the position property as can be seen below. You can also add the code to the Update() method If you wish to keep the GameObject moving continuously

  1. public class Movement : MonoBehaviour
  2. {
  3. private void Update()
  4. {
  5. // Move the object along the X axis
  6. transform.position += new Vector3(1.0f, 0.0f, 0.0f);
  7. }
  8. }

How to Rotate a GameObject

There are several ways to rotate a GameObject in Unity. One way is to change the rotation property of the UnityEngine.Transform component. In the example below, we make use of Euler angles to rotate an GameObject along its Y axis..

  1. public class Rotation: MonoBehaviour
  2. {
  3. private void Update()
  4. {
  5. // Rotate the object along the Y axis every frame.
  6. transform.rotation *= Quaternion.Euler(0.0f, 1.0f, 0.0f);
  7. }
  8. }

How to Start a Coroutine?

A coroutine can be started using the StartCoroutine() method that is a part of the UnityEngine.MonoBehaviour class. You can either start it by using a method returning a IEnumerator directly or by passing in the name of the method. In the example below we are logging a string to the console after a delay of 1 second.

  1. StartCoroutine("Log", "Hello World");
  2. StartCoroutine(Log("Hello World"));
  3.  
  4. private IEnumerator Log(string message)
  5. {
  6. yield return new WaitForSeconds(1.0f);
  7. Debug.Log(message);
  8. }

How to Stop a Coroutine?

Once a coroutine has been started it can be stopped by using the MonoBehaviour.StopCoroutine() method. Optionally, we can call the MonoBehaviour.StopAllCoroutines() method to stop all coroutines that are started by the same MonoBehaviour.

  1. Coroutine routine = StartCoroutine(Log("Hello World"));
  2.  
  3. StopCoroutine(routine);
  4. StopAllCoroutines();
  5.  
  6. private IEnumerator Log(string message)
  7. {
  8. yield return new WaitForSeconds(1.0f);
  9. Debug.Log(message);
  10. }

How to Pause a Scene in Unity?

The easiest way to pause a scene in Unity is by setting the global time scale to 0.0 using Time.timeScale. However, this only works if the Time.deltaTime variable is actively used in your scripted MonoBehaviours. To unpause the game, simply set the global time scale back to the default value of 1.0

  1. public class ScenePause : MonoBehaviour
  2. {
  3. private void Awake()
  4. {
  5. // Pause the game by setting the global time scale to 0
  6. Time.timeScale = 0.0f;
  7. }
  8. }