Why can I access another script even though it is not static? It works but i dont know why?

public class FakeSceneManager : MonoBehaviour
{
public static void ChangeFakeScene(GameObject newFakeScene)
    {
        currentFakeScene.SetActive(false);
        currentFakeScene = newFakeScene;
        currentFakeScene.SetActive(true);
    }
}

this is a class that is attached to a gameobject in my scene and it contains some stuff that i wont show because its to long, it also contains this static method called ChangeFakeScene

in another script i can access this method by using:

FakeSceneManager.ChangeFakeScene(FakeSceneManager.mapFakeScene);

but there is no reference, it works but i dont know why
the class is not static, nor do i have a public field that i set up in the inspector

There’s no reference to what?
What field are you confused about?

You didn’t share all your code so I have no idea what FakeSceneManager.mapFakeScene is.

Classes do not need to be static to access their static members, in this case a static method. Static variables and methods always belong to their class, not instances of the class. A static class simply cannot have instances, and therefore can’t have instance members. From your code above, I can tell that FakeSceneManager has at least two static variables (or maybe properties), currentFakeScene and mapFakeScene. If either of those is not static, then this code would not compile. You can think of static members (variables and methods) as belonging to their class, or as being shared between all instances of the class. The second idea is less accurate, but might be easier to understand at first.

2 Likes

thank you for ur explanation, i understand now