Been trying to get this snippet of a random generation code for a part of my game correct, ALL DAY.
Essentially, I want to create a game of bomb defusal with some wires that you have to cut in a certain way to defuse the bomb. So I have created a piece of code which determines whether there will be 3,4,5 or 6 wires.
double number = Random.value; number = (number * 4 + 3); number = System.Math.Floor (number); Debug.Log (number); string GenerationCode = number.ToString ();
This works. Has been working for 2 days now.
Next, I wanted to create some wires, depending on however many that first snippet of code generated. The wires will all be different, with a wire generated with a number 1 would be red, a wire generated with a number 2 would be orange, a wire with number three would be yellow etc etc etc. Up to 7 different colours. So for instance the first part of the code would decide that there would be 5 Wires and the colours would be Red, Yellow, Indigo, Orange, Blue.
So, I created a temporary variable (TempVar) that would pick a number between 1 and 7 and store it in an array.
But it doesnt work, I have tried all day and Iâve got to the verge 3 or 4 times of snapping my laptop in two. Hereâs my code, I plan after this to works to append these variables in a string so I can generate a random generation code each time my game is played so it doesnât get boring after playing the same level over and over.
See if you can help. If you can help, you have saved my life. I am new to C#, if something is clearly wrong and Iâm an utter idiot then you have the complete right to call me a tool in the replies. Anyway take a look and see what you can see. :0
// Use this for initialization void Start () { double number = Random.value; 10 number = (number * 4 + 3); number = System.Math.Floor (number); Debug.Log (number); string GenerationCode = number.ToString (); 15 Debug.Log (GenerationCode); for (int i = 0; i < number; i++) { float[number] TempVar = (float)Random.value; 20 TempVar = (float)System.Math.Floor ((TempVar * 6) + 1); } } EDIT: Just realised I didnât specify the errors. Lemme list them. IndexOutOfRangeException: Array index is out of range. Assets/Scripta/Output.cs(19,8): error CS1525: Unexpected symbol ['* *Assets/Scripta/Output.cs(19,15): error CS1525: Unexpected symbol ]â
Sorry, I re-did my reply. My first one was way wrong. Sorry, I was on mobile, going from memory, and apparently my memory is bad.
Does this help you get started? Iâm not sure what values youâre loading in your array, but I think this will help get you going:
public class SomeClass : MonoBehaviour {
int[] tempVar; // prepare your array... I made this ints, cause it sounds like you only need whole numbers
int wireCount = Random.Range(3,7); // this will instantiate wireCount with 3, 4, 5 or 6...
void Start(){
tempVar = new int[wireCount]; // instantiate and size our array to the number of wires
for (int i = 0; i < wireCount; i++) // loop through the number of wires...
{
tempVar [i] = Random.Range(1,8); // replace this with whatever value(s) you're loading.. this will iterate through x number of times, you just need to say what to load in..
}
}
}
If this isnât what you were thinking, it might be best to give an example of what youâre trying to get your data to look like, in the end, and then we can show you how to get it that way.
BTW, for the record, hereâs why your code was erroring outâŚ
Say your number variable was equal to 3.
In your loop, youâre constantly setting the TempVar at index 3 to a random value. Obviously, that isnât what you want to do, to begin with, because you arenât changing the index⌠but on top of that, youâre also setting the value on an index that doesnât exist, because if you have 3 elements in your array, then they are actually referenced like so (note that index ZERO is the first element, not index 1):
TempVar[0] â first element
TempVar[1] â second element
TempVar[2] â third / last element
âŚ
TempVar[3] = explosions and complaints from the compiler
On top of that, your data types are incorrect too⌠youâre working with numbers with high precision for no reason (although you were using floor to fix that, but you didnât really need to if you just use ints).
Iâm still learning a lot with Unity, myself⌠the more you use it, the more you go⌠âwow, thereâs already a function for that??â. Sometimes it can be good to just check out the manual for a particular class (in this case âRandomâ) and just see what tools you have available to you!
And definitely check out Methosâ post⌠one of the obvious advantages of using code tags is it makes it way easier for everyone to read. One of the less obvious advantages is it automatically links to documentation for you (click on key words in my code, like âRandomâ, for example!)
Okay, had to configure your code slightly because Random.Range canât be called outside of Start() or Awake() because it has an error, after I moved that single line inside of the Start(), it worked perfectly. Thank you very much. And thank you methos5k. Both of you are amazing <3
It can be called âoutside of Start/Awakeâ, it just canât be called in that area where you declare variables (haha, sorry, canât think of what that would be called). I.e., you just have to have it in a method of some sort, somewhere. Glad you got it sorted though!!!