- Home /
Keeping track of multiple instances of one prefab
I'm instantiating a small number (4 to 5 instances) of the same prefab into a single scene, the prefabs are rigidbodies and are spawned in sequence over time (with a for loop) and all appear in the inspector as "nameofprefab(Clone)"
As each instance is used for a scoring mechanism I need to be able to uniquely identifiy each one for collision purposes but am unsure of how to give each instance a unique ID.
What is the best practice for this? Is there a way I can give each instance a unique name when it's spawned? I've heard of "tags" but not had the chance to read about them yet - although I'm not sure if they are the correct way of solving my problem.
Secondary to this, I also need to instantiate a blob shadow projector (for each instance) which will be told to follow it - currently I attach a script to a blob shadow projector which tells it what gameobject to follow in a pre-determined fashion, I'm a bit unsure of how to automatically instantiate multiple instances of shadow projectors which dynamically are able to follow different instances (hopefully with some kind of unique ID) of the same prefab.
I really hope this makes sense!
Can anyone help with these issues?
Answer by BerggreenDK · Oct 13, 2011 at 05:08 PM
Show us your spawn code and we can easily add it I think.
But to explain it in short.
The line where you use the Instatiate function, keep the result in a GameObject variable. Like:
GameObject goTemp = Instantiate(something)...
Then on the goTemp object, you now how ... tradaaa! .name attribute.
I usually do it like this:
for(int cnt=0; cnt<10 ; cnt++)
{
GameObject go = Instatiate(something);
go.name = "Something_" + cnt;
}
To add the number in the end of the name + I am still able to search for "something_" with the GameObject.Find("something_") function
Hi BerggreenD$$anonymous$$ - your solution was perfect, that's exactly what I was after - I've given it a quick go and it works perfectly - this is my new code (if statement) with your stuff added:
var i : int = 0;
if (Input.GetButtonDown("Shoot")&&i<currentlevel.length&&shotdelay==1&&controller.gameplay==true){
shootdelay();
var instance : GameObject = Instantiate(currentlevel[i], transform.position, transform.rotation);
instance.name = "Shape_" + i;
i++;
}
So my next question would be, how would I instantiate shadow blob projectors for each of these objects?
$$anonymous$$y current shadow projectors have the following script attached:
var objectToFollow : Transform;
var offset : Vector3;
function Update () {
transform.position = objectToFollow.position-offset;
}
...which allows me to tell each one to follow a specific game object, but ideally I need to automatically instantiate them in a way where they know which "instance.name = "Shape_" + i;" gameobject to follow
Answer by chemicalvamp · Oct 13, 2011 at 05:05 PM
If the object has a script attached to it the script can access by transform in example:
void OnCollisionEnter()
{
Destroy(transform.gameObject);
}
would destroy the object when it is collided with.
Answer by poncho · Oct 13, 2011 at 05:11 PM
the way i track my objects is to store the reference of that object in a list of gameobjects
List myGameObjectsToTrack;
myGameObjectsToTrack.Add(thelocalGameObjectInstantiated);
myGameObjects[indexOfMyGameObjectIWouldUse].
using events and the gameobjects themselvels, would be a way to solve your issue