1

I've run into some accessibility problem with one of my Unity projects. I created a class that does NOT inherit from MonoBehaviour. It has a method public static void LoadScene(string sceneName).

When I try to call this method from another class, I get a syntax error. This is my first script with the static method:

public class GameLoader
{
   public static void LoadScene(string sceneName)
   {
       SceneManager.LoadSceneAsync(sceneName);
   }
}

And here is my other script:

public class GameHandler : MonoBehaviour
{
   private void Start()
   {
       GameLoader.LoadScene("MyScene");    //Syntax error
   }
}

Normally, I would have some idea about what might the problem be, but in this case, the GameHandler recognizes GameLoader as class, but after the dot (GameLoader.), it does not find any property or function at all. And I get a syntax error when I try to write anything after the dot.

I experimented a little and it doesn't seem like I would cross another class with the name GameLoader and the neccessary namespace was added as well.

I'm pretty lost here, I hope someone can help me out.

Original codeGameLoader:

using UnityEngine.SceneManagement;
using UnityEngine;

namespace MyGame
{
   namespace System
   {
       public class GameLoader
       {
           public static void LoadScene()
           {

           }
       }
   }
}

Original UIHandler:

using UnityEngine;
using System;

namespace MyGame
{
   namespace System
   {
       namespace UI
       {
          public class UIHandlerMenu : MonoBehaviour
          {
              GameLoader.LoadScene();
          }
       }
   }
}

Error message:

Severity Code Description Project File Line Suppression State Error IDE1007 The name 'GameLoader.LoadScene' does not exist in the current context.

And the same error for just LoadScene itself.

8
  • If you click on the GameLoader script in the assets once, do you see its content in the Inspector? Are there any other compiler errors? And do you get the error in Unity or maybe only in your IDE?
    – derHugo
    Commented Aug 18, 2021 at 12:32
  • Yes, I see its content and I have no other errors.
    – B3NII
    Commented Aug 18, 2021 at 12:33
  • Is GameLoader file saved? Is there a star next to its tab name?
    – fafase
    Commented Aug 18, 2021 at 12:51
  • Everything is saved.
    – B3NII
    Commented Aug 18, 2021 at 12:54
  • Are both classes in the same project or is GameLoader in a referenced dll/project?
    – Steeeve
    Commented Aug 18, 2021 at 12:57

1 Answer 1

1

Edit: After your edit it is now obvious where the problem lies. Try moving your call GameLoader.LoadScene("bla"); into a method. If you want this method getting called when instantiating your handler you can move it to a constructor. Example:

public UiHandlerMenu() {
    GameLoader.LoadScene("bla");
}
8
  • I even changed from VS 2019 to VS 2017 in Unity and still nothing. Of course Unity and VS restarts didn't really work unfortunately.
    – B3NII
    Commented Aug 18, 2021 at 12:54
  • Have you tried removing the code inside of ``` public static void LoadScene(string sceneName) ``` and then cleaning and rebuilding again? Commented Aug 18, 2021 at 13:15
  • Yes, I tried it. I even deleted the script file and recreated it. No results.
    – B3NII
    Commented Aug 18, 2021 at 13:20
  • If you don't need to instantiate the GameLoader class you can try adding static to the class, otherwise I am out of ideas too: public static class GameLoader { ... } Commented Aug 18, 2021 at 13:22
  • 1
    Looking at your edit.. Is GameLoader.LoadScene() really inside the class and not inside of a method/constructor? That is the error Commented Aug 18, 2021 at 13:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.