- Home /
assign different values to prefabs on instantiation
i have a list[] of strings and a number 'j' how many strings there are.
the instantiation of my prefabs uses
for(i=0;i<j;i++; {instantiate(prefab);}
to create exactly as many prefabs as there are strings; that part is already working.
now what i need to do is give the prefabs a value to distinguish them - preferably a string or a int (i), but i'm stuck at the part where to put it.
i added a script to the instantiation with a static variable, but of course it gets overwritten with the other instantiations so that every prefab has the same value in the end.
i also tried to just name the prefab like the string with
myPrefab.name = "String"
, but for some reason Unity still puts a (clone) behind it, which renders the string useless (unless i find a method to cut the (clone) from the string afterwards, but that seems to be a bit long-winded).
would be very grateful for a hint to the right direction because i'm running out of ideas right now.
Answer by aldonaletto · Jul 13, 2011 at 01:52 PM
Instantiate the prefab clone, then rename it:
var newObj: GameObject;
for (i=0;..;..){
newObj = Instantiate(thePrefab...);
newObj.name = "prefab#"+i;
}
i already tried that; strangely Unity puts (clone) behind the name, which renders the string useless (unless i find a method to cut the (clone) from the string afterwards)...
the code is
newObj.name = List[i];
which unity then names List1(clone)
I've just tested it and everything worked as expected: my objects were named prefab#0, prefab#1 etc. Unity appends "(clone)" to any object created by Instantiate. Something in your code should be wrong to give these results - it seems you were instantiating the name "List1" too...
yeah i suspected that there had to be something wrong, but i'm unable to find the fault in my code. this is the code 'as is':
var myShark : GameObject;
private var i : int = 0;
function Start ()
{
var j: int = XmlReader1.j;
for (i=0; i<j; i++)
{
var pos = Vector3(Random.Range(136,140),Random.Range(-5,5),Random.Range(99,103));
Instantiate(myShark,pos, Quaternion.identity);
Debug.Log("i did something! " +i);
myShark.name = XmlReader1.testList[i];
}
}
You're modifying the name of the original, not the one returned from Instantiate.
The ideal way is to assign the object instantiated to a variable, then rename it, like below (Find will waste a lot of time!):
var myShark : GameObject; // this is your prefab
private var shark : GameObject; // this will be instantiated
private var i : int = 0;
function Start ()
{
var j: int = XmlReader1.j;
for (i=0; i<j; i++)
{
var pos = Vector3(Random.Range(136,140),Random.Range(-5,5),Random.Range(99,103));
shark = Instantiate(myShark,pos, Quaternion.identity);
// this shark is the one you've instantiated:
shark.name = XmlReader1.testList[i];
}
}
Answer by Waz · Jul 13, 2011 at 01:33 PM
Put a script on the prefab, with a variable you set:
myPrefab.GetComponent.<MyScript>().myVariable = "String";
thanks for your fast answer! unfortunately all the prefabs have the same string now, but i need it to be different strings/values.
Your answer
Follow this Question
Related Questions
[Solved]Instantiating prefab from a script and destroy it from another one 2 Answers
Network instantiate command do not execute in all client.. 1 Answer
Scripts of instantiated objects 1 Answer
How to have unique instances of script on multiple instances of prefab? 1 Answer
Instantiate Problem 4 Answers