Need help using Random.Range in an if statement

Hey guys, Long story short, what I’m trying to do here is use random.range to select 0 or 1, and depending on whether 0 or 1 is selected, something will spawn. 0 = one object will spawn, 1 = a different object will spawn.

I’m getting the Print from this, but it only ever prints SpawnedA, never ever does it SpawnB, I’m not too sure why.

Here’s the relevant code

void XPSpawnCommon(){
        int randomPlace = Random.Range(0, xpSpawnPoints.Length);
            commonInterval -= Time.unscaledDeltaTime;
        float randomSelection = Random.Range(0,1);

        if(commonInterval <= 0){
            if(randomSelection == 0){
                Instantiate(xp5, xpSpawnPoints[randomPlace].transform.position, transform.rotation);
                    commonInterval = 15f;
                        print("SpawnedA");
            }else if(randomSelection == 1){
                Instantiate(xp10, xpSpawnPoints[randomPlace].transform.position, transform.rotation);
                    commonInterval = 15f;
                        print("SpawnedB");
            }
        }

Never really tried nested if statements and not too sure how they work, in my brain this should work but I obviously don’t fully understand how to use Random.Range like this, i’ve tried it another way without nesting if statements. same thing, only ever SpawnsA

All the required information is in docs Unity - Scripting API: Random.Range

I just discovered this myself, I got a bit confused between floats and ints, as I declare it as a float, I thought that didn’t apply.

float randomSelection = Random.Range(0f,1f);
float randomSelection = Random.Range(0,1);

if I don’t include the f, unity will treat those numbers as ints, so that will always return 0? whereas if I add the f, unity will know it’s a float and return either? have I got that right?

So I could do (0,2) or (0f,1f) and that’ll be the same outcome?

Random.Range(0,2) will return int, 0 or 1, Random.Range(0,1f) will return random float between 0 and 1, so it can be 0.2, 0.33 and so on

Thank you!, exactly what I needed to know. much appreciated. I now am getting


These errors which I’ve absolutely no clue what they are, what they mean, or how to fix. What even is a cast?!

They’re pointing to Instantiate(xp10, xpSpawnPoints[randomPlace].transform.position, transform.rotation);

Casting is converting a variable of one type to another type. You can do that implicitly, for example:

int a = 10;
long b = a;

or explicitly

int a = 10;
long b;
b = (long)a;

It is not possible to cast a type to anything, for example casting int to a GameObject won’t work.

Looks like you are doing it somewhere in Update, please share full error log and a script, so we will be able to help you

It’s quite a mess so I do apologise. but Here is everything relating to this. This is in FixedUpdate

void FixedUpdate(){

        XPSpawnCommon();
        XPSpawnUncommon();
        XPSpawnRare();
        XPSpawnElite();
      
        TimeControl();

      
    }

and then here

void XPSpawnCommon(){
        int randomPlace = Random.Range(0, xpSpawnPoints.Length);
            commonInterval -= Time.unscaledDeltaTime;
        float randomSelection = Random.Range(0,2);

        if(commonInterval <= 0){
            if(randomSelection == 0){
                Instantiate(xp5, xpSpawnPoints[randomPlace].transform.position, transform.rotation);
                    commonInterval = 15f;
                        print("SpawnedA");
            }else if(randomSelection == 1){
                Instantiate(xp10, xpSpawnPoints[randomPlace].transform.position, transform.rotation);
                    commonInterval = 15f;
                        print("SpawnedB");
            }
        }


        // if(Random.Range(0,1) == 0 && commonInterval <= 0){         
        //         Instantiate(xp5, xpSpawnPoints[randomPlace].transform.position, transform.rotation);
        //             commonInterval = 15f;
        //                 print("Spawned 0 common thing");
        // }
        // if(Random.Range(0,1) == 1  && commonInterval <= 0){
        //         Instantiate(xp10, xpSpawnPoints[randomPlace].transform.position, transform.rotation);
        //             commonInterval = 15f;
        //                 print("Spawned 1 common thing");
        // }
      
    }

and here is the console in full

so it’s referencing “XPSpawnCommon();” and the Instantiate (Line 96 & 165)

Well, no… even leaving the type out of the picture, the quantities are not right, not even if you cast it to an int.

Random.Range (0.0f , 1.0f) will almost NEVER round to 1… it will almost be identical to (0,1), eg, almost always zero.

1 Like

Yeah, I sorta forgot how floats work. this is turning my brain to mush!

1 Like

I have it working. It seems somewhere along the line, the inspector lost reference to the gameobjects, so I had to assign them again, and now all seems good. Thanks for your help

1 Like

I think the InvalidCastException can arise this way:

  • you make a public GameObject foo; field

  • you drag a GameObject into foo

  • you save the scene and time passes, nations rise, nations fall.

  • now you decide “let’s rename that to public Transform foo; instead…”

  • Press play… now what WAS a GameObject is now slotted into “foo” and will try to be hammered into what is now a Transform. Mass hysteria ensues.

2 Likes

That pretty much explains it!

1 Like