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
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.
– numberkruncherAh i see. Much better. Only thing is now unity says 'System.Random' does not contain a definition for `Range'? That's not right...
– anon55762765It should be
– numberkruncherRandom.RangenotSystem.Random(see http://unity3d.com/support/documentation/ScriptReference/Random.html) You should also have the namespaceUnityEngine
– anon55762765Random' is an ambiguous reference betweenUnityEngine.Random' and `System.Random'Fixed it. Thanks a ton for the help! I really appreciate it. :)
– anon55762765