Unity Spams Errors When Nothing is Wrong with the Code

78 compilers errors from the switch case and the for loop.

Most of these are for the instantiates in the switch case. They seem to be correct for me though. I searched through the unity doc and compared it.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WorldGeneration : MonoBehaviour
{
    public GameObject D1;
    public GameObject D2;
    public GameObject D3;
    public GameObject D4;

    for (float i = 0; i < 10f; i += 1)
    {
        float r = Random.Range(1, 4);
        switch(r)
        {
            case 1:
                Instantiate(D1, new Vector3(i * 1.5f, 0, 0), Quaternion.identity);
                break;
            case 2:
                Instantiate(D2, new Vector3(i * 1.5f, 0, 0), Quaternion.identity);
                break;
            case 3:
                Instantiate(D3, new Vector3(i * 1.5f, 0, 0), Quaternion.identity);
                break;
            case 4:
                Instantiate(D4, new Vector3(i * 1.5f, 0, 0), Quaternion.identity);
                break;
        }
    }
}

b83scq

Most of the errors are repeats of ; expected, type expected, indentifier expected, invalid token “)”, invalid token “,”, etc.

I just don’t see anything wrong with the instantiate.

Your for loop isn’t in a function. It’s at the class level, which makes no sense.

Your use of floats instead of integers for the forloop and switch are also not correct.
You’re using them like they are integers, so they should be integers. Floats don’t work like integers.

2 Likes

But I don’t think the float thing will cause a “red dot” syntax error or even make the program run wrong (there’s no imprecision issues if you add 1.0 each time). That first thing is the only problem: “this needs to be in the function void Start() {}”.

It’s as if you started telling a stranger about all of the drugs your cat is taking. The problem isn’t that you’re telling it wrong – it’s that you didn’t start talking about cats and medicine with them first.

No C# does not allow float to be used in switch statements. It “might” be allowed if used with relational pattern matching, unsure as I never tried this myself, but the normal switch statement requires discreet values. and floats/doubles can’t be guaranteed to always be the exact same value on all processors, so the language doesn’t allow it

My point is – just put it inside Start(){}. All of those nonsense errors will go away and worse case, you’ll get just a few new errors that actually say what the problem is. There’s nothing wrong with pointing out other problems, but new coders get overwhelmed.

Code_cat: ignore this. Often people get off in their own side-topic, and this is that.

It turns out that switch-ing on a float that way is legal – things like case 3.6f: and even case 1: (on a float argument). Switches are one of those things that compiler-writers just love writing new weird stuff for, and C# seems to be no exception. The C# docs merely say that version 6.0 and below are limited to integral types. I didn’t see where they explicitly say 7.0+ can use straight-up float compare, but my old version of Unity+visualCode is cool with it. It doesn’t seem that useful, and it seems like they’ve at least give a warning (they don’t). Instead they couldn’t resist making it legal.

The thing about float imprecision is that it’s only because the fractional part is made of 1/2, 1/4, 1/8, 1/16 and so on. 0.1f can’t be built exactly out of those, nor can most fractional values. That’s the imprecision. But floats have no problem with whole numbers – why would they? 1.0f is stored perfectly as 1 in the whole number part 0 in the fractional. Likewise 3.0f and 105f easily store the whole number perfectly, with 0 as the fraction. 1.0f+5.0f+3.0f is exactly perfectly 9.0f. I’ve never tried it, but I suspect 1.5f+4.5f is perfectly 6.0 (since they store “1” in the 1/2’s place for the fraction). That’s mostly useless, but, for example, using MoveTowards(x,5.0f) will eventually result in exactly x==5.0f.