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?
using UnityEngine; public class CreateObject : MonoBehaviour { void Start() { GameObject newObj = new GameObject("Player"); Debug.Log(newObj.name); } }
Look at how the GameObject is created and what name is passed to its constructor.
The GameObject constructor takes a string parameter that sets the name of the object. So the name will be exactly "Player".
In Unity C#, which of the following methods correctly creates a new GameObject and sets its name to "Enemy"?
Check which constructor or method allows passing a name string directly.
The GameObject constructor can take a string to set the name immediately. Option B creates a GameObject but sets the name after creation, which is valid but not the direct method asked.
Examine the code below. Why does it throw a NullReferenceException at runtime?
using UnityEngine; public class CreateAndName : MonoBehaviour { void Start() { GameObject obj; obj.name = "NPC"; } }
Think about what happens if you try to use a variable that has no object assigned.
The variable obj is declared but not assigned any GameObject instance, so it is null. Trying to access obj.name causes a NullReferenceException.
Which of the following lines of code correctly creates a new GameObject named "Boss" in Unity?
Remember the syntax for calling constructors in C#.
Option A uses the correct constructor syntax with parentheses and a string argument. Option A misses parentheses, C uses a non-existent method, and D tries to call name as a method.
Analyze the following Unity C# code snippet. How many GameObjects are created and what are their names?
using UnityEngine; public class MultiCreate : MonoBehaviour { void Start() { GameObject a = new GameObject("Alpha"); GameObject b = new GameObject(); b.name = "Beta"; GameObject c = new GameObject(); } }
Remember the default name assigned when no name is set explicitly.
The first GameObject is named "Alpha" by constructor. The second is created unnamed but then named "Beta". The third is created unnamed and keeps the default name "New GameObject".