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

25
SabreRael•

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.

1
OurOnlyWayForwardOP•

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?

2
SabreRael•

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.

1
tyrellLtd•

Is it absolutely necessary to use a for loop to get a random index? Why not use Random.Range? Example: int randomIndex = Random.Range(0, yourGOList.Count -1);. If you're applying some specific rules, like don't roll the same number twice, you could always create an auxiliary pool of available indices and remove them if necessary, and a method to reset the pool under some specific circumstance.

In another comment you mentioned enabling or disabling the game objects with setActive in Update and the performance. Why in Update? And it seems like you'd still be polling your bool array, on every frame.

You could reduce the number of calls with an event/delegate based system or if you keep the polling approach, by adding a logic gate (something like if (!indexChanged) { return; })

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.

Okay, so if I assumed correctly, you have an array "A" of integers with length 7, and an array "B" of booleans, also with a length of 7. If both arrays are ordered in the same way (which you probably want to make sure of), then the indices in both would match, that is, they would correspond with one another. When you roll an index 4 in arrayA, arrayB[4] would be the corresponding boolean value. You could also just use a List and if you know it's length or count you can get a random from it like I mentioned above, or a dictionary if you must, where the keys would be the indices from the first array.

This is the very explicit version on how that match would work with arrays, which can be shortened a lot:

	int[] indices = { 0, 1, 2, 3, 4, 5, 6 };
	bool[] flags = { false, false, false, false, false, false, false };

	int randomIndex = Random.Range(0, indices.Length -1);
	flags[randomIndex] = true;

	//or if you want to only have one enabled at a time, here's one not so great example how to do it:
	//for (int i = 0; i < flags.Length; i++)
	//{
	//	if (i == randomIndex)
	//		flags[i] = true;
	//	else
	//		flags[i] = false;
	//}

Your priority though, would be defining what is your "master" list, i.e., which table defines the order of things. Is it the indices? Is it the game objects? Then the arrays of indices (not really necessary) and flags would be derived from that, so all rows in the 1, 2, or 3 tables would match.

1
OurOnlyWayForwardOP•

I got started with a for loop to get the random number result by following this brief video https://youtu.be/OUlxP4rZap0

Which you can see there how he works with game objects rather than bools within a script so he can just drag and drop

The bool list ultimately toggle on/off a set of score multipliers that constantly affect the text on the screen (score is always rising). So I hit it every frame in update which I feel is a bad idea but figured it’s necessary for accurate scoring. It’s also a text based game so it’s hard to tell when I’m taking performance hits due to bad decisions since as a fairly simple text game, it always runs well)

The code you mentioned in the second half is kind of where I’ve ended up. Just matching them since they correspond in their index. I don’t think I’ll need to add more objects any time soon but I’m always worried about making stuff hard to change later

1