Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Savrun · Nov 26, 2018 at 02:29 PM · unity 2d

randomly pick a value from a tuple

Hello, i need to pick a value from a tuple (-1.0f, 1.0f)

i could seem like, if exist, may be:

value = Random.someting(tuple(-1.0,1.0)) ....

value assigned with -1.0 or 1.0 for example.

Or is there any other alternative way? Thank you ..

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by ModLunar · Nov 26, 2018 at 02:40 PM

I'm not sure if I'm understanding correctly, but say for example, if you have a Vector3, and you wanted to randomly pick one of its xyz components:

You can actually index into Vector3s (along with Vector2s, Vector4s, Vector2Ints, and Vector3Ints) as if they are arrays (thanks to C# indexers!). So for example, you could do this:

 using UnityEngine;
 
 //...
 
 Vector3 tuple = new Vector3(12, -15, 18);
 
 //Note:
 //tuple[0] is 12
 //tuple[1] is -15
 //tuple[2] is 18
 //Trying to access tuple[3] would throw an IndexOutOfRangeException, since there are only 3 components to a Vector3 and tuple[3] would be the fourth.
 
 //Get a random integer in range [0, 2], meaning 0, 1, or 2. This will be used as an index into the Vector3.
 int randomIndex = Random.Range(0, 3);
 
 //result will be either 12, -15, or 18.
 float result = tuple[randomIndex];



Doing the same thing, written shorter, you can do:

 using UnityEngine;
 
 //...
 
 Vector3 tuple = new Vector3(12, -15, 18);
 float result = tuple[Random.Range(0, 3)];
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
avatar image
0

Answer by Savrun · Nov 26, 2018 at 02:47 PM

Ah, cool. i'll get the index randomly. thank you.

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
avatar image
0

Answer by s_awali · Nov 26, 2018 at 03:00 PM

As Tuple are introduced in C#7.0, and Unity use C#3.5 (or 4.0 in preview), Tuples are not available.

Still, you can use a Vector2 to achieve the same effect. Then, you can use Random.Range(minValue, maxValue); to randomly pick a value between minValue and maxValue.

/!\ If minValue and maxValue are integers (int), then maxValue is exclusive /!\

See more

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

99 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 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 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 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

Finding relative mouse position 2 Answers

Do I need to destroy objects I'm not using? 4 Answers

Seeing different amounts of the game in different resolutions 0 Answers

how to add 2d cutscenes to unity 2 Answers

Possible to change size of cursor with Cursor.SetCursor? 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