Home > OS >  How should I write a proper Switch statement so all objects don't Instantiate at once?
How should I write a proper Switch statement so all objects don't Instantiate at once?

Time:10-06

I have resources loading when a specific function is called. However all of the objects are being Instantiated and not just the specific one I need. I would like some help writing a proper switch statement.

Here is my code.

   if (File.Exists(objPath) == Resources.Load("BlueCube"))
    {
        //FileUtil.CopyFileOrDirectory(objPath, savePath);
    GameObject instance = Instantiate(Resources.Load("BlueCube", typeof(GameObject))) as GameObject;
    }

    if (File.Exists(objPath) == Resources.Load("RedCube"))
    {

    GameObject instance = Instantiate(Resources.Load("RedCube", typeof(GameObject))) as GameObject;

    }

    if (File.Exists(objPath) == Resources.Load("GreenCube"))
    {
    GameObject instance = Instantiate(Resources.Load("GreenCube", typeof(GameObject))) as GameObject;
    }
    if (File.Exists(objPath) == Resources.Load("PurpleCube"))
    {
    GameObject instance = Instantiate(Resources.Load("PurpleCube", typeof(GameObject))) as GameObject;
    }

CodePudding user response:

well ... File.Exists(objPath) returns a bool and Resources.Load("BlueCube") returns the loaded UnityEngine.Object which has an implicit conversion to bool

=> All your if conditions can be true at the same time

CodePudding user response:

For a switch-case to work, you need to have something you can check for different cases. In your case, it seems you want to instantiate cubes of different colors. Introducing an enum can help with readability and extensibility.

Create a new enum that includes all your colors, you can easily add new colors later. Next, instead of using objectPath, you want to use that enum.

Say your function currently looks like this:

private void InstantiateCubeAtPath(string objPath)

You want to change it to:

private void InstantiateCubeWithColor(CubeColor CubeColor.green)or whatever Color you want. You also have to need to add the information about its Color to each cube.

In your switch case you now check for CubeColor and proceed from there.

Hope that helps!

CodePudding user response:

In the if statements you are trying to compare a boolean result from File.Exists(objPath) with an object type from Resources.Load("Any").

File.Exists(string path) just says if the file exists.

Resources.Load("AnyCube") returns a GameObject in the context.

What filenames are passed through objPath? Do you have some sample names? Then it is easier to show a real switch case here.

I can imagine that the file names have the name that should be loaded here as a resource. Then you could solve that with a conditional switch.

GameObject instance;
    switch (objPath)
    { 
         case string op when op.Contains("BlueCube"):
             instance = Instantiate(Resources.Load("BlueCube", typeof(GameObject))) as GameObject;
             break;
         case string op when op.Contains("RedCube"):
             instance = Instantiate(Resources.Load("RedCube", typeof(GameObject))) as GameObject;
             break;
         case string op when op.Contains("GreenCube"):
             instance = Instantiate(Resources.Load("GreenCube", typeof(GameObject))) as GameObject;
             break;
    }

But a switch case can be used with the scope, but if more different game objects are loaded, then it is better to start with an open-close principle.

  • Related