Modifying private variables

I can’t really think of how to define this problem, either way it is something that I haven’t ran into the answer yet and it would expedite my production to have the answer.

So in this specific case I have a platform which will simulate grass, among all the functions inside the script, the platform holds a variable “currGrass.” It keeps a count of how much grass is currently on the platform, and until currGrass is == maxGrass it will continue to regrow/instantiate the grass prefab on the grass platform.

I then have fire that when collided with the grass (not the grass platform, but the grass pieces) will activate the blades of grass script and change its textures/set it on fire/burn it/remove it/etc. When I burn the grass I obviously want to reduce the currGrass count so it can regrow another piece of grass.

At the moment I have it so that the grass is a child of the platform, and its using the get child.count of child “burned” and “alive” grass, and when the grass is dead I have it move to the burned section, but it’s all rather obnoxious doing it this way, though it does keep it organized it would be nice to not have to use this get child count feature especially because there are a couple other instances where it wouldn’t work as well.

The whole package is a prefab so I can’t use a static variable or public variable or any of that stuff, I am sure that I am just missing something, but if anyone could point me in the right direction that would be great, thanks.

Why can’t you use a public variable? Being a prefab isn’t relevant to that.

–Eric

And public variables aren’t necessarily static variables, all public means is that it can easily be checked or modified by other scripts with a reference to this script.

I think you got public mixed up with static.

Yes, static means there is only one instance of that variable; it’s not an access modifier like public/private/etc.

–Eric

Ok well I hate to bump this thread because it’s been answered a thousand times but I just can’t get this to work. I spent another couple hours on Google along with trying to use a public variable and I just cannot find a way to access the variable.

I tried using a public variable but couldn’t find a way to use it other than through the hierarchy.
I tried using some GetComponent<>(); of the script, which didn’t do anything to help.
I tried some thing in some guide where you made an instance of the script, in this case it was public GrassPlatform grassplatform, then using grassplatform.burnedGrass after assigning my platform to the public variable but that didn’t actually change it.

Like I said, I must just be missing some core component to how this works, because I just do not understand how to communicate between scripts reliably.

I have no problems accessing scripts when using like, a static variable, but obviously that isn’t going to work here, any further assistance is much appreciated.

GetComponent with public variables is correct. The docs have a bunch of examples. Really not much to it, just get a reference to the desired GameObject if necessary, then use that with GetComponent.

–Eric