x


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.

more ▼

asked May 28 '12 at 12:11 AM

Drakestar gravatar image

Drakestar
916 6 7 14

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered May 28 '12 at 12:45 AM

Datael gravatar image

Datael
555 2 5

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).

May 28 '12 at 01:03 AM Drakestar

In that specific case then yes, I would agree to allow the object to rename itself after it has set itself up, but it all depends on for which purpose you are renaming it. I would argue, though, that the issue of code maintenance is not because you'd have to copy paste the naming code at every place you are instantiating it, but I would say the root of that problem is that you are instantiating it from multiple places. If it were me and I had the same prefab being instantiated from different places I would make some kind of manager that can be called upon from multiple places into which you pass each instantiated item's specifics.

For example, in the current project I'm working on we are using a generator to spawn text that appears and then floats away whenever a bonus is received. This text is being created from many different locations, but to have the instantiation code in many different locations, too, would be a very bad thing; instead we are sending all relevant data to a manager that does the rest for us:

// Apologies for the C#
// I realize your original question was in JS

public class FloatawayTextGenerator : MonoBehaviour {

    [SerializeField] FloatawayText floatawayTextPrefab;
    [SerializeField] Transform hostTransform;

    public void SpawnFloatawayText(string text, Vector3 rootPosition, FloatawayText.IconType iconType = FloatawayText.IconType.None, FloatawayText.FontColor fontColor = FloatawayText.FontColor.Black) {
       var newText = (FloatawayText)GameObject.Instantiate(floatawayTextPrefab);
       newText.gameObject.name = "FloatawayText (" + text + ")";
       newText.transform.position = rootPosition;
       newText.transform.parent = hostTransform;
       newText.BeginAnimation(text, iconType, fontColor);
    }
}

Now if there is anything we need to change about this game object or how it works internally we don't have to change the instantiation code everywhere.

Another thing that this system would allow would be easy re-use of the prefabs to save over-instantiating. Trying to manage a cache of game objects when they're all being instantiated in different places would be a nightmare. Using a manager, however, would make it a trivial task and no code would have to be changed except for the code inside the manager.

May 28 '12 at 03:59 AM Datael

I don't disagree :)

May 28 '12 at 04:17 PM Drakestar
(comments are locked)
10|3000 characters needed characters left

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 + "]";
    }
more ▼

answered May 28 '12 at 12:14 AM

Drakestar gravatar image

Drakestar
916 6 7 14

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

May 28 '12 at 12:55 AM Eric5h5

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".

May 28 '12 at 01:04 AM Drakestar
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1672
x323
x1

asked: May 28 '12 at 12:11 AM

Seen: 1250 times

Last Updated: May 28 '12 at 04:17 PM