I have a GameObject called GameManager. I have made it static. It also has DoNotDestroyOnLoad(). Is there a way I can access it by saying GameManager.DoThis() from any scene?
Once Try this
public class GameManager : MonoBehaviour {
public static void DoSomeThing(){
Debug.Log("I will Call this method from another script...");
}
}
Another Script
public class AccessMethod : MonoBehaviour {
// Use this for initialization
void Start () {
GameManager.DoSomeThing();
}
}
This is called the singleton pattern. Its typically done like this.
public GameObject instance;
void Awake (){
if(instance){
Destroy(gameObject);
return;
}
instance = gameObject;
DontDestroyOnLoad(gameObject);
}
You can go one step further and make instance a property that will create the GameObject the first time it is called (lazy loading).
Note that you cannot have a true static GameObject. You can only fake it.