Bird
Raised Fist0
Unityframework~20 mins

Creating and naming GameObjects in Unity - Practice Exercises

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
GameObject Creator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Unity C# code?

Consider the following Unity C# script attached to an empty GameObject. What will be the name of the newly created GameObject printed in the console?

Unity
using UnityEngine;

public class CreateObject : MonoBehaviour {
    void Start() {
        GameObject newObj = new GameObject("Player");
        Debug.Log(newObj.name);
    }
}
A""
B"GameObject"
C"Player"
D"newObj"
Attempts:
2 left
💡 Hint

Look at how the GameObject is created and what name is passed to its constructor.

🧠 Conceptual
intermediate
2:00remaining
Which method creates a GameObject with a specific name in Unity?

In Unity C#, which of the following methods correctly creates a new GameObject and sets its name to "Enemy"?

AGameObject enemy = GameObject.Create("Enemy");
BGameObject enemy = new GameObject("Enemy");
CGameObject enemy = new GameObject(); enemy.name = "Enemy";
DGameObject enemy = GameObject.Instantiate("Enemy");
Attempts:
2 left
💡 Hint

Check which constructor or method allows passing a name string directly.

🔧 Debug
advanced
2:00remaining
Why does this code throw a NullReferenceException?

Examine the code below. Why does it throw a NullReferenceException at runtime?

Unity
using UnityEngine;

public class CreateAndName : MonoBehaviour {
    void Start() {
        GameObject obj;
        obj.name = "NPC";
    }
}
ABecause name is a read-only property and cannot be set.
BBecause "NPC" is an invalid name for a GameObject.
CBecause Start() method cannot create or name GameObjects.
DBecause obj is declared but never initialized, so obj is null when setting name.
Attempts:
2 left
💡 Hint

Think about what happens if you try to use a variable that has no object assigned.

📝 Syntax
advanced
2:00remaining
Which option correctly creates and names a GameObject in one line?

Which of the following lines of code correctly creates a new GameObject named "Boss" in Unity?

AGameObject boss = new GameObject("Boss");
BGameObject boss = new GameObject; boss.name = "Boss";
CGameObject boss = GameObject.Create("Boss");
DGameObject boss = new GameObject(); boss.name("Boss");
Attempts:
2 left
💡 Hint

Remember the syntax for calling constructors in C#.

🚀 Application
expert
3:00remaining
How many GameObjects are created and what are their names?

Analyze the following Unity C# code snippet. How many GameObjects are created and what are their names?

Unity
using UnityEngine;

public class MultiCreate : MonoBehaviour {
    void Start() {
        GameObject a = new GameObject("Alpha");
        GameObject b = new GameObject();
        b.name = "Beta";
        GameObject c = new GameObject();
    }
}
A3 GameObjects: "Alpha", "Beta", "New GameObject"
B3 GameObjects: "Alpha", "Beta", "GameObject"
C2 GameObjects: "Alpha", "Beta"
D3 GameObjects: "Alpha", "Beta", "" (empty string)
Attempts:
2 left
💡 Hint

Remember the default name assigned when no name is set explicitly.

Practice

(1/5)
1. What does the following code do in Unity?
GameObject player = new GameObject("Hero");
easy
A. Creates a new script called "Hero".
B. Deletes the GameObject named "Hero" from the scene.
C. Renames an existing GameObject to "Hero".
D. Creates a new GameObject named "Hero" and assigns it to variable player.

Solution

  1. Step 1: Understand the new GameObject creation

    The code uses new GameObject("Hero") which creates a new GameObject with the name "Hero".
  2. Step 2: Assign the new GameObject to a variable

    The created GameObject is stored in the variable player for later use.
  3. Final Answer:

    Creates a new GameObject named "Hero" and assigns it to variable player. -> Option D
  4. Quick Check:

    new GameObject("Name") creates and names GameObject [OK]
Hint: new GameObject("Name") creates and names in one step [OK]
Common Mistakes:
  • Thinking it renames an existing object
  • Confusing creation with deletion
  • Assuming it creates a script
2. Which of the following is the correct syntax to create a GameObject named "Enemy" in Unity?
easy
A. GameObject enemy = new GameObject("Enemy");
B. GameObject enemy = new GameObject(Enemy);
C. GameObject enemy = GameObject("Enemy");
D. GameObject enemy = new GameObject['Enemy'];

Solution

  1. Step 1: Check the constructor syntax

    The GameObject constructor requires a string name inside double quotes, so "Enemy" is correct.
  2. Step 2: Validate the assignment

    The new GameObject is assigned to variable enemy using the = operator, which is correct.
  3. Final Answer:

    GameObject enemy = new GameObject("Enemy"); -> Option A
  4. Quick Check:

    Use quotes for string names in new GameObject() [OK]
Hint: Use double quotes for names in new GameObject("Name") [OK]
Common Mistakes:
  • Omitting quotes around the name
  • Using parentheses without new keyword
  • Using square brackets instead of parentheses
3. What will be the name of the GameObject created by this code?
GameObject obj = new GameObject("Tree");
Debug.Log(obj.name);
medium
A. Tree
B. null
C. GameObject
D. obj

Solution

  1. Step 1: Create GameObject with name "Tree"

    The constructor new GameObject("Tree") sets the GameObject's name property to "Tree".
  2. Step 2: Print the name property

    Debug.Log(obj.name) outputs the name of the GameObject, which is "Tree".
  3. Final Answer:

    Tree -> Option A
  4. Quick Check:

    obj.name equals the name given in constructor [OK]
Hint: obj.name matches the string used in new GameObject("Name") [OK]
Common Mistakes:
  • Thinking obj.name is the variable name
  • Assuming default name 'GameObject'
  • Confusing output with variable name
4. Identify the error in this code snippet:
GameObject enemy = new GameObject(Enemy);
enemy.name = "Orc";
medium
A. Cannot assign name after creation.
B. Missing quotes around Enemy in new GameObject.
C. Variable enemy is not declared.
D. GameObject cannot be created with new keyword.

Solution

  1. Step 1: Check the constructor argument

    The argument Enemy is missing quotes, so it is treated as a variable, causing an error.
  2. Step 2: Confirm name assignment is valid

    Assigning enemy.name = "Orc"; is allowed and correct after creation.
  3. Final Answer:

    Missing quotes around Enemy in new GameObject. -> Option B
  4. Quick Check:

    String names need quotes in new GameObject("Name") [OK]
Hint: Always put quotes around the name string in new GameObject() [OK]
Common Mistakes:
  • Forgetting quotes around string names
  • Thinking name can't be changed after creation
  • Confusing variable declaration errors
5. You want to create three GameObjects named "Enemy1", "Enemy2", and "Enemy3" using a loop. Which code correctly creates and names them?
hard
A. for (int i = 1; i <= 3; i++) { GameObject enemy = new GameObject("Enemy" + "i"); }
B. for (int i = 1; i <= 3; i++) { GameObject enemy = new GameObject(Enemy + i); }
C. for (int i = 1; i <= 3; i++) { GameObject enemy = new GameObject("Enemy" + i); }
D. for (int i = 1; i <= 3; i++) { GameObject enemy = new GameObject("Enemy" * i); }

Solution

  1. Step 1: Check string concatenation in loop

    for (int i = 1; i <= 3; i++) { GameObject enemy = new GameObject("Enemy" + i); } correctly concatenates the string "Enemy" with the integer i using + operator, producing "Enemy1", "Enemy2", "Enemy3".
  2. Step 2: Validate syntax and naming

    for (int i = 1; i <= 3; i++) { GameObject enemy = new GameObject("Enemy" + i); } uses quotes correctly and creates new GameObjects with the intended names inside the loop.
  3. Final Answer:

    for (int i = 1; i <= 3; i++) { GameObject enemy = new GameObject("Enemy" + i); } -> Option C
  4. Quick Check:

    Use + to join strings and numbers for names [OK]
Hint: Use "Enemy" + i to name GameObjects in loops [OK]
Common Mistakes:
  • Omitting quotes around "Enemy"
  • Using variables without quotes
  • Trying to multiply strings
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard