I’m trying to make my random code run twice so it can selection to random players. It works for 1, but I can’t get it to work with 2 players. Here’s my code for choosing a random player.
void TagRandomPlayer(){
int PlayerListRange = PlayerList.Count;
System.Random Rand = new System.Random ();
int RandomPick = Rand.Next (0, PlayerListRange);
TaggedPlayer = PlayerList [RandomPick];
KnifeWeapon knife = PlayerDictionary [TaggedPlayer].GetComponent<KnifeWeapon>();
knife.GetComponent<KnifeWeapon>().enabled = true;
}
Hi, consider using an function where you can call so that it picks a random value for you when you need it instead of having to call it everytime the function runs. Also, try using Random.Range instead (I prefer it :P)
It will would look something like this:
public string[] names = new string[] { "Hello", "Bye", "Cya" };
string TaggedPlayer;
void Start ()
{
TagRandomPlayer();
}
void TagRandomPlayer()
{
TaggedPlayer = names[PickRandom()];
print(TaggedPlayer);
}
int PickRandom()
{
int PlayerListRange = names.Length;
int RandomPick = Random.Range(0, PlayerListRange);
return RandomPick;
}
Now it selects an random value for you at the and prints it at start! This shouldn’t be to hard to add =)