- Home /
Check if with a spawned gameobject
I have this line of code that destroys a child of a prefab (works) and also I want to check for a certain spawned prefab and increase my gold by 20 if it dies (does not work).
if (ai && !ai.dead){ // if ai.dead not set yet...
ai.dead = true; // set it and destroy capsule
Destroy(transform.Find("Capsule").gameObject);
if (gameObject.name == "RightGrunt(Clone)"){gold += 20;}
}
Apparently it doesn't work because it is a spawned prefab. If I do this with the same prefab set in the scene it works. How can apply the if for the spawned prefab ?
Code is in C#
Where is that "gameObject" co$$anonymous$$g from? If it's the object your script is attached to, then you already know its name (unless the same script is attached to multiple kinds of prefabs, but that would be a bit silly) and you can just increment gold. I guess I'm not understanding why you're doing that name check.
I have the same script for all of my prefabs because it is a general health.cs script. I decided to have the check here for gold add, because I have no idea how to do it otherwise.
Ah, I see. Well, if "RightGrunt(Clone)" is indeed the name of the Prefab Instance, which you can check in the Hierarchy tab while running, then your code should work.
$$anonymous$$aybe try with tags ins$$anonymous$$d. Add a tag to your Prefab and check for that ins$$anonymous$$d. (Adding tags is confusing if you haven't done it yet. See the Tag $$anonymous$$anager page.)
I know how to work with tags. I will try this as well and return with feedback.
Still not working. And I can;t manipulate the tag as it is used for another script... changing it would screw that one.
Is there a workaround? How can I check what prefab died so I can give a certain amount of gold ?
Answer by primus88 · May 22, 2013 at 08:41 AM
Anyone can shed some light on why this is happening ?
How is it possible that the code works for a prefab already in the game, but not for the same prefab that is spawned ? For days I;m thinking at this and I have no idea..
Your answer
Follow this Question
Related Questions
Spawn a prefab 1 Answer
Setting Position of Spawned Prefab 2 Answers
Help: script that worked don't work with Unity 3.5 0 Answers
Spawning a projectile with a trigger 1 Answer
Spawn System Troubles 0 Answers