0
0
Unityframework~20 mins

Creating and naming GameObjects in Unity - Practice Exercises

Choose your learning style9 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.