|
(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.
(comments are locked)
|
|
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).
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: 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)
|
|
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: 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)
|
