What is going on? I cannot solve this problem

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).

Wow. Let me run this and see if I can fix my problems :0

If that fixes it, great… :slight_smile:
If you have further questions, please take a quick look at this post:

It’s for when you add code in the forums, and it will show up like @cstooch 's post :slight_smile:
It’s much nicer to read/follow.

Oh. I totally understand now, I didn’t realise that Random.Range existed. xD. You’ve saved my life. +Rep to you in the highest degree <3

1 Like

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??”. :slight_smile: 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!)

Didn’t know you could do that and will do that in future. Thank you very much. I hate to be a nuisance for people :slight_smile:

We all had to learn it at some point. :slight_smile: Good luck with your game!

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

1 Like

Cool - great, glad you got it working :slight_smile:

Oops, forgot about that!

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!!!