- Home /
Selecting Random Object and Setting Variable to True
Hello guys, so for my game I need a random object to be selected. It's not exactly how it sounds. What I have in my script is "var CPU : AIScript[]"
So all the CPU/AI objects that have the script "AIScript" are all in one array.
Then what I need is to randomly select one of those objects in the array. The "AIScript" has a variable "var Tagged : boolean = false"
What I need is for the script to randomly select an object in the array, and within that object, make sure that Tagged = true.
I'm guessing there's an easy way to do this but right now I'm stuck :P
Thanks In Advance!
Answer by Ejlersen · Dec 30, 2010 at 07:59 PM
You can do something like this, if I understand the question correctly:
// Get random index
var randomIndex : int = Random.Range(0, CPU.Length);
// Set its tagged value to true
CPU[randomIndex].Tagged = true;
Ah k, I was thinking of doing something like this but I didn't know how to do exactly do it :P Thx
Answer by PrimeDerektive · Dec 30, 2010 at 08:03 PM
Probably something like this:
randomAI = CPU[Random.Range(0,CPU.length)];
randomAI.GetComponent(AIScript).Tagged = true;
Your answer
Follow this Question
Related Questions
Check if var is true from a random object 0 Answers
Random time activate/deactivate object 1 Answer
Object selection 1 Answer
Spawn Random Enemy 3 Answers