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 post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by abhishakverma · Jan 11, 2014 at 02:10 PM · javascript

How can stop the repeat random number (Random.Range)

How can stop the repeat random number. i am going to paste my code.please have a look.

 #pragma strict
 
 
 var NameArray : String[] = ["apple","kite","knife","man","rat","name"];
 var chosenName : String;
  
 function ChooseName()
 {
 
 chosenName = NameArray[Random.Range(0, NameArray.length)];
 //var chosenName = NameArray[Mathf.floor(Random.Range() * NameArray.length)];
 Debug.Log("chosenName " + chosenName);
 
 
 }
  
 function OnGUI()
 {
 if (GUI.Button(Rect(10, 10, 100, 35), "random")) {ChooseName() ;}
 GUI.Box (Rect(120, 10, 100, 35), "" + chosenName);
 }
 
 
 function Start(){
 InvokeRepeating ("ChooseName", 0, 10); 
 
 //ChooseName();
 
 

}

Comment
Add comment · Show 1
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 Kumo-Kairo · Jan 11, 2014 at 02:49 PM 0
Share

What does it mean "repeat"? You have several object to name? Or is it always the same name every time you start a game?

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by agies1 · Jan 11, 2014 at 04:03 PM

If I understand correctly you are looking to get a non-repeating random name. We can try something where we remove the selected name from the array each call. If we run out of names we reset the collection from the original template.

 var NameArrayTemplate : String[] = ["apple","kite","knife","man","rat","name"];
 // Using Unity JS Lists
 var NameArray : List.<String> = new List.<String>(NameArrayTemplate);

 function chooseAName() : String{
     var index = Random.Range(0, NameArray.Count);
     chosenName = NameArray[index];
     NameArray.RemoveAt(index);
     if (NameArray.Count == 0)
         NameArray.AddRange(NameArrayTemplate);
     return chosenName;
 }
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 agies1 · Jan 13, 2014 at 11:53 AM 0
Share

Because I switched to a List, the property should be Count and not length, sorry.

avatar image abhishakverma · Jan 13, 2014 at 01:17 PM 0
Share

Thanks a lot agies1.Now its work fine.

avatar image Bunny83 · Jan 13, 2014 at 02:01 PM 0
Share

I fixed several typos.

avatar image
1

Answer by robertbu · Jan 11, 2014 at 03:27 PM

The typical solution is to first shuffle the array (randomly swap elements for some number of iterations) in Start(). Then you select them in order until the list is empty. Alternately you can put your strings in a generic list and remove the string from the list as it is selected.

See the generic list section here:

http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use?

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

20 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

Related Questions

Make tranform follow raycast 1 Answer

Pressing Two Buttons At The Same Time it not working 0 Answers

Problems making character hang on walls 0 Answers

function OnCollosionEnter problems 1 Answer

UNITY3D: TEXTFILE I/O : Don't write same things 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