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.

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.

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;
	}
}