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 Noah-1 · Apr 17, 2014 at 12:26 AM · prefabobjectrandomrange

Avoid getting same object as previous one with Random.Range?

Hey guys, been working with Random.Range today and I am trying to avoid getting the same result as the previous one when i randomly select an object from an array, here is the code:

 var myArray : GameObject[]; //array
 
 function Start (){    
 myArray = gameObject.FindGameObjectsWithTag ("point");//fill array with objects     
 }
 
 function Update(){
 if (Input.GetButtonDown("Fire1")){
 RandomPick();
 }
 }
 
 function RandomPick(){
 var myIndex = Random.Range(0, myArray.length);//randomly pick an object
 Debug.Log(myArray[myIndex]);//output selected object
 }

What would be the simplest way of preventing myIndex from selecting the same object as the previous one?

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 Owen-Reynolds · Apr 17, 2014 at 06:46 AM 0
Share

Same as any previous item, meaning never a repeat? Or just not the immediate previous -- can't pick the same thing twice in a row, but could pick it every other?

avatar image Noah-1 · Apr 17, 2014 at 08:18 PM 0
Share

I meant not the immediate previous.I already solved the problem by creating another array and every time one object was selected, it was placed on the second array for 5 seconds, preventing the Random.Range from immediately selecting it again.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by fendorio · Apr 17, 2014 at 04:15 AM

Remove it from the array? Other than that what you want isn't really random..

My JS is rusty to be fair haha!

  function RandomPick(){

          var myIndex = Random.Range(0, myArray.length);//randomly pick an object
          Debug.Log(myArray[myIndex]);//output selected object

          myArray.splice(myIndex,1); //removes the index:myIndex
  }

So yeah splice does the work here:

  splice(index, numberOfTimes);

index = The index of the element you want to remove numberOfTimes = Number of items to be removed - 0 results in 0 items being removed.

If you want to store the object you just removed instead of 'just' removing it, simply add:

  var x = myArray.splice(myIndex,1);
Comment
Add comment · Show 2 · 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 cool_freak · Apr 21, 2020 at 07:59 AM 0
Share

Array doesn't show splice function

avatar image Noah-1 cool_freak · Apr 21, 2020 at 08:05 AM 0
Share

I have no idea why my 6 year old question has been getting attention today. This code snippet was written in Unityscript, a previously supported program$$anonymous$$g language made for Unity. Unity dropped support for it and for Boo a long time ago. Try using something like foos.RemoveAt(index); ins$$anonymous$$d

avatar image
0

Answer by endasil_unity · Apr 20, 2020 at 02:10 PM

I would solve it like this if it is just the last value you do not want repeated.

 var myArray : GameObject[]; //array
 // Keep track of last selected index
 var previousPick = -1; 
 function Start () {    
     myArray = gameObject.FindGameObjectsWithTag ("point");
  }
  
 function RandomPick(){
     var myIndex = previousPick;
   // loop until the random value does not match last one
    while(myIndex == previousPick)
     {
         myIndex = Random.Range(0, myArray.length);
     }
    // update last one.
     previousIndex = myIndex;
 }
Comment
Add comment · Show 5 · 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 Noah-1 · Apr 20, 2020 at 02:48 PM 0
Share

Hey man, thanks for helping 6 years ago me.

avatar image endasil_unity Noah-1 · Apr 20, 2020 at 03:11 PM 0
Share

Haha better late than never they say! :P I only checked posts on the first two pages, i wonder how something 6 years old arrive there? I assumed they would be ordered by time. Do they randomly pop up questions without anything marked as correct answer? :)

avatar image Bunny83 endasil_unity · Apr 20, 2020 at 04:45 PM 0
Share

No, someone posted a quite irrelevant comment stating that an array doesn't have a slice method -.- That comment was actually rejected by moderators but the bump is already there. The only way I know to revert such a bump is by reporting the comment and deleting it through the reported posts page. This will remove the bump. $$anonymous$$oving a post to moderation and rejecting it does not undo the bump since technically the post is still there, just not visible.


Note that your answer has two issues:

  • First UnityScript is a thing of the past. $$anonymous$$ost people using Unity use a more or less recent version which does no longer support UnityScript. So it's generally better to provide code samples in C# now.

  • Using a while loop like that has a potential risk for causing a hang (soft crash) of your application. This happens when the array just contains a single element (or even zero). It's in general dangerous to use any code like this without any safety measures. It might work now but since it depends on external data (specifically gameobjects with the "point" tag) this is out of the control of this code.

Show more comments

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

25 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

Related Questions

Instantiate a random prefab at an objects location 3 Answers

Random Range with limits 2 Answers

How to assign Transform to prefab ? 2 Answers

Scene object to prefab 1 Answer

Instantiating a random prefab from array. 1 Answer


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