- Home /
Adding booleans to array and randomize
I am making a program where I select player that will be playing and adding them to a array, but ti won't add them to the array. And I also want to randomize the active players in the array. What code do I use, is it Random.Range or others?
This is the code I am using:
#pragma strict
public var number_of_players : int;
var playercounter : GUIText;
//Players that will join
var player1 : boolean = false;
var player2 : boolean = false;
var player3 : boolean = false;
var player4 : boolean = false;
var player5 : boolean = false;
var player6 : boolean = false;
var myarray : boolean[] = new boolean[5];
function Start () {
number_of_players = 0;
Debug.Log(number_of_players.ToString());
}
function Update() {
if (player1 == true) {
player1 = myarray[0];
}
}
function OnGUI () {
playercounter.guiText.text = number_of_players.ToString();
player1 = GUI.Toggle (Rect (210,140,130,20), player1, "Player 1");
player2 = GUI.Toggle (Rect (210,160,130,20), player2, "Player 2");
player3 = GUI.Toggle (Rect (210,180,130,20), player3, "Player 3");
player4 = GUI.Toggle (Rect (210,200,130,20), player4, "Player 4");
player5 = GUI.Toggle (Rect (210,220,130,20), player5, "Player 5");
player6 = GUI.Toggle (Rect (210,240,130,20), player6, "Player 6");
}
Thanks in advance
You never assign any elements to the array. No array or array element is ever to the left of an assignment operator in your code.
Also, if you have every would-be element of the array hard-coded, but you want them in an array, just ditch the individual player1, player2... variables altogether and use your array-of-bools ins$$anonymous$$d. The bool element at index zero will replace the "player1" variable, and so on.
myArray[Random.Range(0, myArray.Length)] will return a random array member.
So, I just change the name of the "0" index in the array in the inspector and just refer to it as "myarray[0]" or do I have to refer to it as the new name "myarray[player1]". This might be a stupid question but I am quite new. Thanks in advance
Your answer
Follow this Question
Related Questions
boolean issue 1 Answer
Shotgun using array/list in Javascript 1 Answer
Convert Array into One String (Js) 1 Answer
Reading and Storing External Data into Memory (From Text) 1 Answer