• 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 myjean17 · Feb 29, 2012 at 11:25 PM · c#arraygame

Picking a Random Order for Levers

I am trying to create a system in which the script will contain an array of levers, each having a DIFFERENT 'ID' number, which will represent the order it must be pulled in. Unfortunately, it does not work and I need help determining why. The 'ID' numbers are often the same, while I am aiming for them all to be different. (There is 3 levers, by the way, although the script is designed to work with any number.)

The script currently iterates through the array of levers, and for each lever, randomly picks an integer, assigns it to a separate array of ints, and then iterates through that array, with a while statement that makes sure the new number is not the same as any of the previous ones. After all of this, the final integer is assigned to the "orderID" variable of the lever.

Basically put, it doesn't work.

Heres my code: (I might also mention it is written in C#)

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class LeverPuzzle : MonoBehaviour {
     
     public List <GameObject> levers;
     public List <GameObject> leversInOrder;
     
     public List <int> orders;
         
     private int leverCount;
 
     void Start () {
         
         leverCount = levers.Count;
                 
         foreach (GameObject lever in levers) {
             
             int randomInt = Random.Range (1, leverCount + 1);
             
             var leverScript = lever.GetComponent <LeverScript>();
                         
             orders.Add (randomInt);
                         
             foreach (int order in orders) {
                 
                 while (randomInt == order) {
                     
                     randomInt = Random.Range (1, leverCount + 1);
                     
                 }
                             
             }
             
             leverScript.orderID = randomInt;
             
         }
     
     }
     
 }

Any help is greatly appreciated. Thank you in advance!

-myjean17

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

1 Reply

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

Answer by numberkruncher · Mar 01, 2012 at 12:40 AM

It might be easier to create a random number dispenser class that will only dispense each number once, but in a random order:

Warning: Not tested but should work

 using System;
 using System.Collections.Generic;

 public class RandomNumberDispenser {
 
     public readonly int from;
     public readonly int to;
     
     private List<int> _numbers;
 
     // Initialise with range from 1 to x
     public RandomNumberDispenser(int to) : this(1, to) {}
     
     // Initialise with inclusive range, i.e. 1 to 10
     public RandomNumberDispenser(int from, int to) {
         this.from = from;
         this.to = to;
         
         _numbers = new List<int>();
         for (int n = from; n <= to; ++n)
             _numbers.Add(n);
     }
     
     // Return next random number
     public int Next() {
         // Select list index at random
         int i = Random.Range(0, _numbers.Count);
         int n = _numbers[i];
         
         // Remove number from list
         _numbers.RemoveAt(i);
         
         return n;
     }
     
     // Indicates if there are any more random numbers to dispense
     public bool hasFinished {
         get { return _numbers.Count == 0; }
     }
 
 }
 
 
 // USAGE
 
 RandomNumberDispenser dispenser = new RandomNumberDispenser(levers.Count);
 foreach (GameObject lever in levers) {
     LeverScript leverScript = lever.GetComponent<LeverScript>();
     leverScript.orderID = dispenser.Next();
     
     // If you still want to create an array of orders
     orders.Add(leverScript.orderID)
 }
Comment
Add comment · Show 6 · 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 numberkruncher · Mar 01, 2012 at 12:41 AM 0
Share

Sorry there was a typo in my source. For some reason the edit feature on this website does not cause text to be updated so I have reposted my answer.

avatar image myjean17 · Mar 01, 2012 at 12:51 AM 0
Share

Ah i see. $$anonymous$$uch better. Only thing is now unity says 'System.Random' does not contain a definition for `Range'? That's not right...

avatar image numberkruncher · Mar 01, 2012 at 01:00 AM 0
Share

It should be Random.Range not System.Random (see http://unity3d.com/support/documentation/ScriptReference/Random.html) You should also have the namespace UnityEngine

avatar image myjean17 · Mar 01, 2012 at 01:13 AM 0
Share

Random' is an ambiguous reference between UnityEngine.Random' and `System.Random'

avatar image myjean17 · Mar 01, 2012 at 01:14 AM 0
Share

Fixed it. Thanks a ton for the help! I really appreciate it. :)

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

A problem with intersection detection 1 Answer

Array index out of range error 1 Answer

Few problems with arrays 1 Answer

A problem with arrays 1 Answer

Toggle Control between multiple turrets 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