- Home /
How to use random.range twice
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;
}
Unity has a premade random roller, Random. Your title has it, but your code doesn't use it??
Can use Random.Range(1,7);
anywhere (which rolls 1-6, as usual.) See the docs for other random shortcuts they added.
Otherwise, this is more a "help me with this code" Q. Since you roll two randoms by just doing it: int a1=Random.Range(0,players), a2=Random.Range(0,players);
I did what you said and the other code is activated instantly.
void TagRandomPlayer(){
int PlayerListRange = PlayerList.Count;
int a1=Random.Range(0, PlayerListRange), a2=Random.Range(0, PlayerListRange);
TaggedPlayer = PlayerList [a1];
TaggedHero = PlayerList [a2];
SecretWeapon gun = PlayerDictionary [TaggedHero].GetComponent<SecretWeapon> ();
gun.GetComponent<SecretWeapon> ().enabled = true;
$$anonymous$$nifeWeapon knife = PlayerDictionary [TaggedPlayer].GetComponent<$$anonymous$$nifeWeapon>();
knife.GetComponent<$$anonymous$$nifeWeapon>().enabled = true;
}
IEnumerator OnNewRound(){
yield return new WaitForSeconds(30);
TagRandomPlayer ();
Debug.Log ("Hello");
}
Also it choose the same person randomly
Rand.Next() will return a value inclusive of the lower bound but exclusive of the upper bound which I think is your problem (working for 1 but not for the second player)
http://msdn.microsoft.com/en-us/library/2dx6wyd4%28v=vs.110%29.aspx
Ah I had mistakenly thought it was only choosing the first player repeatedly, a mistake I'm just after making with Unity's random.range function sorry.
I have not used the yield coroutines for unity and im not entirely sure what
yield return new WaitForSeconds(30);
does but the document may have the correct syntax you need
// Prints 0
print (Time.time);
// Waits 5 seconds
yield WaitForSeconds (5);
// Prints 5.0
print (Time.time);
http://docs.unity3d.com/ScriptReference/WaitForSeconds-ctor.html
Answer by Byt3 Spl0it · Dec 29, 2014 at 10:39 PM
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 =)
Your answer
Follow this Question
Related Questions
script always instantiates monster even when its not supposed to? 1 Answer
How do I use Get Component in this code 1 Answer
(Photon) When I move the first player the second player moves as well. Why? 1 Answer
Can someone explain this part of the PlayerMoveController.js script in the AngryBots demo? 3 Answers
Enabling a component on initialization/Photon Cloud Problem 0 Answers