So this question has been asked many times… but has never quite answered my question.
how would i access script data (such as a Vector3 array) from an instantiated Object. I have done a junk of research and found out that instantiated Objects end in the word “clone”. also the use of “Tags” using GameObject.FindWithTag is also an option. because scripts and hierchies also get copied I assume you can also assign variables to these scripts. so my list of related questions…
-
If an object is instantiated multiple times do the objects have some sort of numbered extension? (eg rocketClone.001 like a duplication in blender or other 3d modelling apps)
-
when instantiating can I write Data to a script that was also instantiated with it?
-
how efficient is the use of “Tags”. if this were viable would it be more efficient then the use of an Array/Collection to store the data…?
To access the data of an instantiated object… put the instance into a variable…
i.e
var gameObjectInstance = GameObject.Instantiate(myPrefab, Vector3.zero, Quaternion.identity) as GameObject;
// Now get a script called MyPrefabScript on the instantiated object..
var prefabScript = gameObjectInstance.GetComponent<MyPrefabScript>();
Then make sure the Vector3 array/variable is public, and you can access it as such…
prefabScript.Vector3ArrayVariableName = ??
etc…
So if I understand correctly, You need to make your own variables for instancing, such that if you wanted to make a fleet of ships accessible, each ship would hold a spot in an array. (excuse my bad java script as I program in C#)
var gameObjectInstance = [10];
for(int i=1; i=10; i++){
var gameObjectInstance *= GameObject.Instantiate(myPrefab, Vector3.zero, Quaternion.identity) as GameObject;*
}
// Now get a script called MyPrefabScript on the instantiated object…
var prefabScript = gameObjectInstance[2].GetComponent();
prefabScript.Vector3ArrayVariableName = ??
and this would (ignore my syntax errors) give you a reference to the 2nd gameobject’s instanced script. I am assuming you could record values to this in the same loop you used to instance them all aswell?