For some reason, I cannot change the name of a game object (an avatar) but I need to have 2 different versions of it and activate them in code.
So, duplicating the avatar is not an option, but I still need to create an identical one and make a change in editor mode, so that based on a condition, whether one or the other is chosen.
It occurred to me, now that changing the name is not possible, perhaps I can give the other a different tag and activate the right one based on the selected scenario.
So, I checked out a few manual docs and some forum posts and came up with the following 3 alternatives to place in my Start()
but none works:
void Start()
{
if ( GameObject.FindGameObjectWithTag("John") )
gameObject.FindGameObjectWithTag("John").SetActive(true);
if ( GameObject.FindWithTag("John") )
gameObject.FindWithTag("John").SetActive(true);
}
Can someone please help me understand what I am doing wrong and how I can achieve this: find an object that is not active (from among 2 inactive objects with the same name but different tags: one untagged
and the other tagged as, say, John
)
Couldn't be easier, just assign them to different variables
Note, as a rule when you are learning, do not use Find. It's that simple. Use public variables and drag to connect in the inspector.
(If anyone reading does not yet know how to drag to connect in the inspector, check any basic tutorial.)
If you do want to find all objects with a certain tag:
If you want to find one of those with a certain name you have to search through them all..
If you do want to find all objects with a certain name...
It's very important to understand that
...you just can't do that in Unity - it simply makes no sense.
Life's life that -
Find
is a hack you should never really use anyway, other than in rare situations. If you (for some reason?) need to "find" all game objects with a certain name, you have to simply look through every single thing in the scene (which is not particularly easy to do). You can easily google this. But there is, absolutely, no reason you would ever do this.Note that, just one example .. say you had a number of "cat" thing in your scene. Very simply - nothing more complicated than this - you would have them all in the same "folder". (That is to say, all under the same holder game object.) That is just one incredibly simple approach to utterly avoid the sort of problem you're having.
Again there literally is no way to find "all game objects with the same name" - the
Find
call is a hack that should never have been in Unity; they explicitly don't haveAnd most importantly don't forget...Find doesn't work if inactive!
The
Find
call is a hack that should never have been in Unity: always remember it does not even work on inactive objects. Inasmuch as they threw in a "find name" concept, it's probably for the best that it doesn't work on inactive objects: but the whole thing is a shambles - just don't use "find", ever.Particularly as a beginner just drag in the inspector - it's honestly that easy. (And again, in almost all cases your stuff will start inactive, so of course you have to do that since you can't "find" inactive stuff.)
(And a note - don't forget that if you're simply finding a "boss" type script, say your
Scores
orSoundEffects
, never use find. Just do this easy idiom. )