Naming instantiated objects

(I figured out the answer as I was typing the question. Rather than deleting it, I decided to post this for reference to anybody else wondering, because it wasn’t immediately obvious to me.)

Is there a way to give a name to instantiated objects (for debug purposes more than anything)? By default, instantiated GameObjects show up as “PrefabName(Clone)” (in the hierarchy) and “PrefabName(Clone)(UnityEngine.GameObjects)” (in the debugger). I’d like to the instance to get a custom name, very much like debuggers tend to use the ToString() method to describe runtime objects being debuged.

4 Answers

4

You can set the name of the object as soon as you instantiate it; you don’t need a separate script. Just use the return from Instantiate() to get a reference to the new object and change the game object’s name component there.

That's true, but doesn't work when you want the name to include information that's specific to the instance (fields that are initialized in the same Start() method, for example). It also complicates code maintenance - if the project instantiates from multiple locations, each location needs to duplicate the name assignment to name the object consistently (or there needs to be an extra Instantiate function that does both, but that's inconsistent with the Unity convention, and I might as well do it in Start() if I'm gonna write an extra function).

I don't disagree :)

public GameObject objPrefab; // connected in inspector to prefab

{
       private void Start()
      {
           var newObject = (GameObject) Instantiate(objPrefab, Vector3.zero, Quaternion.identity);
           newObject.name = "New";
      }
}

The simple answer to this is to set gameObject.Name to whatever string is desired in the Start() method of the script that’s attached to the prefab. Might seem obvious, but it wasn’t to me at first. For example:

public class Spot : MonoBehavior
{
    private void Start()
    {
        gameObject.name = "Spot at [" + this.Col + ", " + this.Row + "]";
    }

That's actually a complicated answer; see Datael's answer for something simple.

Agreed that the other answer is simpler, but it has problems (see comment below). Keeping the naming consistent when instantiating from multiple locations is the kicker for me to make this a bit more "complicated".

I might be a noob so please apologize. I came across this post in search of a way of naming a bunch of instantiated tiles on map generation grid thingy I’m making. Anyway, I noticed all instantiated objects have the “(Clone)” suffix attached to the Prefab name immediately after creation, which in itself creates a unique circumstance. At the moment of the instantiation, it is currently the only prefab with the (Clone) suffix available, so i made a
" public GameObject tempTile; " to go along with my
" public GameObject TilaA; " and my
" public GameObject TileB; " (I’m making a chess board kind of setup), and then immediately after the instantiation i ran
" tempTile = GameObject.Find(TileA.name + “(Clone)”); " or TileB for the alternative. Now with the temporary reference of the newly cloned tile, I simply used the method
" tempTile.name = “whatever”; " (I actually used this => ( “[ X: " + xPos + " | Z: " + zPos + " | AorB ]” ) which I’m also probably making a very terrible use of the string engine instead of creating an array, but I’m trying here!). The explicit reference to the newly made object seemed to clean up any issues… for now…