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 /
This question was closed May 27, 2014 at 11:50 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by ObeliskEntertainment · May 26, 2014 at 11:22 AM · raycastvector3touchcountgettouch

Random.Range with Lists problem.

I have updated my question as I have narrowed out my problem. I can't seem to be able to store a TOTAL of 4 Vector3 Positions and use Random.Range to randomly select one of them. I previously used the Vector3 position = vectorPositionList [Random.Range (0, vectorPositionList.Count) ];

It worked sort of. When I ran the script more times than the amount of Vector3's I had stored in the List I got an "Out of Range" error. It needs to be able to run in a loop somehow.

Comment
Add comment · Show 2
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 KiraSensei · May 26, 2014 at 12:09 PM 0
Share

This can happen if you add some elements in vectorPositionList between two calls of your Random.Range line.

avatar image ObeliskEntertainment · May 26, 2014 at 12:34 PM 0
Share

I don't seem to be able to understand what you are saying. :(

3 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by ObeliskEntertainment · May 27, 2014 at 05:38 AM

I solved the problem myself. I used the typical Random.Range to create a number, and inserting it directly into an equation like this.

 int randomGenerated = Random.Range (0,4);
 this.selected = this.vectors [randomGenerated];


Gets the job done well, looks clear and is short. I love it! I used tw1st3d 's idea and tweaked it to make it work properly. The ".NEXT" method did not work, it threw out an error.

Comment
Add comment · Show 1 · 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 tw1st3d · May 27, 2014 at 11:45 PM 0
Share
  • for posting solution.


Glad I could help. Not sure what the error with .Next() was, because I can't recreate it, but, whatever :P

avatar image
1

Answer by Chris_Dlala · May 26, 2014 at 01:44 PM

You are using the combination of Random.Range and List.Count correctly. When passed two integers you will get a random number between the min and max but never max because the max is exclusive. However, if you have a list with a count of 0 (an empty list), you will get an error:

 List<Vector3> list = new List<Vector3>(); // An empty list
 int index = Random.Range(0,list.Count); // Count will be 0
 Vector3 v = list[index]; // This error

This code will throw an OutOfRangeException because count is 0 and there is no entry at index 0. To avoid this you will need to check if the list is empty (eg list.Count == 0) before getting a random entry. I hope that helps =)

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 tw1st3d · May 26, 2014 at 01:17 PM

 using UnityEngine;
 using System;
 using System.Collections;
 
 public class Vector3Test : MonoBehavior
 {
     private Vector3[] vectors = new Vector3[4];
     private Vector3 selected;
     
     public void Awake()
     {
         // Create your four vectors
         this.vectors[0] = new Vector3(0, 0, 1, 1);
         this.vectors[1] = new Vector3(1, 1, 0, 0);
         this.vectors[2] = new Vector3(1, 1, 1, 1);
         this.vectors[3] = new Vector3(0, 0, 0, 0);
     }
     
     public void Start()
     {
         // Generate random number
         Random rnd = new Random();
         int picked = rnd.Next(0, 3);
         // Use random number to pick the vector3
         this.selected = this.vectors[picked];
         // Log the randomly picked vector
         Debug.Log(this.selected.ToString("F3"));
         
     }
 }
Comment
Add comment · Show 3 · 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 tw1st3d · May 26, 2014 at 01:18 PM 0
Share

Also, directly to your code error,

 Vector3 position = vectorPositionList [Random.Range (0, vectorPositionList.Count) ]; 

should read

 Vector3 position = vectorPositionList[ Random.Range(0, (vectorPositionList.Count - 1)) ]; 
avatar image Chris_Dlala · May 26, 2014 at 01:53 PM 0
Share

In your examples you will never get the last entry in the list/array because the second parameter in Unity's Random.Range and System.Random.Next are both exclusive.

avatar image ObeliskEntertainment · May 26, 2014 at 07:54 PM 0
Share

All looks good except for .Next throws me an error.

UnityEngine.Random does not contain a definition for Next

and by adding using Systems; It makes Random throw me an error as well.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

Why multiply a vector by -2? 1 Answer

How to get an offset of a RaycastHit? 0 Answers

Get the position of the first finger that touched the collider? 1 Answer

Moving a 3D object with 2D swipes 2 Answers

raycast and trigonometry 2 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