- Home /
Changing the Variables of An Instanced Script
I've got a prefab that has an attached VariableScript with these variables:
public int value = 0;
public bool isVisible = false;
public bool isMine = false;
public bool isFlag = false;
I have another script that accesses these variables and changes them:
variable_script = mineFieldArray[x, y, z].GetComponent<VariableScript>();
variable_script.isMine = true;
'mineFieldArray' is a 3D array that holds each instanced predfab, each with its own VariableScript.
However, when I run the script, I've found that it changes the value of 'isMine' to 'true' for all of the instanced scripts. Am I doing something wrong? It appears to be changing the value in the original script (i.e. for the prefab) itself rather than a particular instance.
Answer by FortisVenaliter · Aug 18, 2015 at 08:44 PM
How do you fill the array? If you are generating the instances at loadtime or runtime, then you need to make sure you are assigning the array values from the instantiated object, not the prefab you are instantiating them from.
This is the code that creates the instances and fills the array (3x3x3):
for (x = 0; x < 3; x++) {
for (y = 0; y < 3; y++) {
for (z = 0; z < 3; z++) {
(Instantiate ($$anonymous$$e, new Vector3 (myX, myY, myZ), Quaternion.identity) as GameObject).transform.parent = gameObject.transform;
$$anonymous$$eFieldArray[x, y, z] = $$anonymous$$e;
myZ += 2;
}
myY += 2;
myZ = -2;
}
myX += 2;
myY = -2;
}
I suspect that it's something to do with the 'Instantiate' line, as I took a look at the prefab after running the code and found I had changed the prefab's variables aswell.
Yeah, that's the problem. "$$anonymous$$e" is presumably set to the prefab, correct? Instantiate returns a value that is the new instance. So you want something like this:
$$anonymous$$eFieldArray[x,y,z] = Instantiate(...
Ah right, that makes sense! That works now. Thanks for the help!
Answer by DizzyWascal · Aug 19, 2015 at 02:31 PM
You can try instantiating your gameObjects into a node and use the GetComponentsInChildren to obtain them into arrays.
using UnityEngine;
using UnityEngine.UI;
public class Example: MonoBehaviour
{
//Make sure this parent gameobject is in the scene because we're instantiating the child
//objects and then parenting them to it. OR you may already have a location for these objects.
public GameObject parent;
public GameObject childObjects;
//The script array you want to get
private ExampleScript[] exampleScript; //Make sure this is attacked to the child objects
void Start()
{
//Just a scenario when these objects should be instantiated
//Like instantiate 20 times I guess
for(int i = 0; i < 20; i++)
{
//Instantiate your objects
Transform childObj = Instantiate(childObjects, parent.position, parent.rotation) as Transform;
//Parent each spawned child to a parent object within each loop
childObj.SetParent(parent, true);
}
//After your objects have been spawned, grab the script you want from them inside the parent node
exampleScript = parent.GetComponentsInChildren<ExampleScript>();
//You now have an array of the script.
//You can now change the variables
exampleScript[0].exampleVariable = true;
}
}
Your answer
Follow this Question
Related Questions
How do I re-link an object that has unintentionally become detached from prefab? 0 Answers
Script uses transform.position of Prefab instead of Instance 1 Answer
How (and where) does Unity save the modified parameters of a prefab's instance? 1 Answer
Two Instances of same Prefab dealing damage to each other. 0 Answers
How do I count how many instances of a prefab there are? 3 Answers