Scripting and the Inspector, how do I link bools with gameobjects?
Hopefully I am asking this right.
I have a Table/array of 7 elements in a script, with a random number for loop that ultimately lands on an element.
The element is just a number however so I created a List of GameObjects. I did this because seems standard, I can drag objects into the table then. But my bools that I want to ultimately toggle on/off are in a single other script on a single GameObject
I am having a hard time figuring out how to go from where my int[] table element result lands, to activating a bool on another script. I’m not sure the list is necessary anymore
Posted Oct 31, 2021, 3:01 PM UTC
So you have a list of 7 GameObjects that you randomly select from. I'm confused what the relation this list has to the bools in the other script. Are the bools stating which GameObject needs to be instantiated or which are active?
Posting your scripts would help give us a better idea of how to help.
I have my main script “GameMechanics” and within it are 7 bools, each relating to one of seven ‘score generators’.
In a second script I have a for loop that using a random number, selects an element from an int array. And I had a List of gameobjects that I thought were involved but as I continue to play with it, I guess aren’t necessary. Bringing in new game objects to setactive true/false and check them in update to assign the bool value, probably is a bad idea.
So in short my problem is taking the element that’s chosen from the array, and saying “I want the corresponding bool in GameMechanics script to turn on/off” so if element 5 is picked, bool5 toggles
I can’t find out how to translate element -> bool in this state. I’m currently trying to take the 7 bools and make those their own array, and so then I can go
Int array -> match corresponding index position in Bool array-> toggle that.
Seems like a solution but also seems redundant?
I'm still confused why you have two scripts. Without more context, there doesn't appear to be a reason for the for loop to select the element(s) to enable/disable to be in a separate file as that just adds complication.
To translate element to bool, the easiest way would be use a Dictionary. A Dictionary will let you map each element to a bool value. Each element becomes an index to set and retrieve the book value for that element.
ETA:You'd initialize one like this:
Dictionary<int, bool> elements = new Dictionary<int, bool>(); elements[0] = false;and so on for all seven of your elements.