I’ve got a game object which needs to talk to a script attached to another gameobject “SceneManager” (which is always tagged “GameController” in each scene).
The name of the script attached to the SceneManager will change in different scenes but i want to write a script for the other game object which will retrieve variables from the script attached to the SceneManager regardless of how the name of the script changes. There is only one script attached to the game object SceneManager and only one SceneManager per scene loaded.
This is how far i’ve currently gotten with my code. I can save the name of the script (“GameControllerScriptName”) but i can’t just do SceneManager.GetComponent<GameControllerScriptName>()
to get the script.
Obviously if the name of the script was constant what does work is:
SceneManager.GetComponent<ActualScriptName>()
But the actual script name changes across scenes. This is my current Code:
public GameObject SceneManager; //This object has the script attached which we want to retrieve.
public MonoBehaviour GameControllerScriptB; //I've tried to get the script using the variables below
public Component GameControllerScript; //
public string GameControllerScriptName;
void Awake () {
SceneManager = GameObject.FindWithTag("GameController");
GameControllerScriptB = SceneManager.GetComponent<MonoBehaviour>() as MonoBehaviour;
GameControllerScriptName = GameControllerScriptB.GetType().ToString();
GameControllerScript = SceneManager.GetComponent(GameControllerScriptName);
}
I’m using Unity 2017.2.0f3 and c#.