- Home /
How to modify a variable on some instances of a prefab in an array of instances?
In essence my question is: for an array of prefabbed game object instances how do I change a variable on a script attached to the prefab on only some of the instances at a time?
I have an array of prefabbed game object square instances that, together, act as the floor which a character can walk on. Every time the char moves he jumps to the next square. Some of these squares are available to be walked on, some are not. The player can trigger groups of the unavailable square instances to become walkable. This part works fine.
I am now trying to make it so that this change to available is only temporary. My plan is to attach a script to the square prefab with a variable. When a group changes to available the variable goes up 3 for that group. Every time the character moves the variable for all square instances goes down one. When it reaches zero on any instance that instance switches back to not available.
I think part of my difficulty lies in the fact that only some of the instances are changing at a time, which ones depend on other things so they do not change in consistent groups. I need to be able to add to the variable on only some of the instances in the array at a time, but I'm not sure how to go about doing it.
I'd like to put up the code, but I'm not sure how without a lot of extra that is necessary to the selection of the squares, but not this particular problem.
Thank you for any help.
Answer by Scoutas · Jan 12, 2017 at 12:01 AM
Make the script that's attached to the floor contain an ID for that tile. Set the ID of the tile, when you are instantiating those objects (e.g. if you're using a loop for (i = 0; i < tileCount; i++)
set the ID to that i
value or something along these lines). Now, because those tiles have ID's you can easily access them inside of the array. just by passing the ID of the tile.
Now, once you try and make the tiles available, get the ID's of the tiles that you want to change, then loop through these ID's (dunno, put them inside of another array?) and pass them to the tileArray and change the variables you want to change inside of them.
Then, once your player moves, start a loop, that would go through every single object in the tileArray and decrease that variable for availability by one.
You could have a script that would run continuously, to check if they're available or not, but I'd suggest running a script that checks it only when you move, after the decrease happens.
I can write up a code example, and comment it out if you want to, but I suggest trying to implement it yourself.
Thank you for the reply. I'm trying to figure out a better way to ask my question. $$anonymous$$aybe I'm just looking at the problem wrong. I assumed in my initial post that the problem had something to do with the way I was trying to access the script that is being used by many objects, but maybe that's not it. For the most part I'm actually ok accessing the individual instances without the IDs. I can manipulate or get info about them individually. The way I'm struggling is in accessing the script on the instances. I know how to access variables and functions from different scripts in general, but it seems like this must require a different syntax or process than I regularly use.
The code below is what I'm using and it gives me a syntax error that freezes Unity making me force quit and restart it. "FaceInstance" is a gameobject in the scene. "FaceScript" is the script attached to it.
private FaceScript faceScript;
void Start ()
{
faceScript = GameObject.Find ("FaceInstance").GetComponent<FaceScript> ();
}
I use this code, with other names, to access script on other game objects in the scene without issue. I've also tried taking it out of the Start function and putting it after all the instances are in place.
Thanks for the help.
Your answer
![](https://koobas.hobune.stream/wayback/20220612104636im_/https://answers.unity.com/themes/thub/images/avi.jpg)