Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by lamecheesykiwi · Dec 29, 2014 at 05:19 PM · photonrandom.range2 player

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;
     }
Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Owen-Reynolds · Dec 29, 2014 at 05:40 PM 1
Share

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);

avatar image lamecheesykiwi · Dec 29, 2014 at 06:06 PM 0
Share

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

avatar image Oliver1135 · Dec 29, 2014 at 06:18 PM 0
Share

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

avatar image lamecheesykiwi · Dec 29, 2014 at 06:19 PM 0
Share

What about my new code?

avatar image Oliver1135 · Dec 29, 2014 at 06:27 PM 0
Share

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

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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 =)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

29 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges