- Home /
Instantiating prefab question.
If I instantiated a prefab twice in my scene. So basically I have two clones of my prefab. Is there a way I can differentiate between them through script? If yes, how?
To be more clear, I am raycasting two rays. I want to be able to tell if both rays hit the same clone, or if each hit a different clone.
Answer by Dave-Carlile · Oct 25, 2013 at 02:30 PM
You can give them each a unique name, or add some sort of ID property in one of the attached scripts and set the ID to a unique value.
That wont work in my case since I spawn a lot of them during the game. It is not just restricted to two. Is there a way by just using the clones I instantiated?
Please respond with comments, not answers - I fixed that for you...
You should be able to just compare the objects. The equality operator returns true if the variables reference the same object, and false if not.
You can do the unique ID pretty easily as well...
public static int NextID = 1;
After instantiating a prefab..
prefab.GetComponent<SomeScriptComponent>.ID = NextID++;
You can then just compare the IDs of the objects. But you should be able to just compare the instances as I mentioned earlier.