Hi,
I’ve been trying to figure out how to load a level by referencing an int that’s a variable in a different object’s script, but I’m doing something wrong.
I’m passing in the name of the int through the inspector and trying to use reflection:
private GameObject manager;
public void ClickAsync(string levelName)
{
manager = GameObject.Find("GameManager");
int levelValue = (int)manager.GetType().GetField(levelName).GetValue(manager);
loadingImage.SetActive(true);
StartCoroutine(LoadLevelWithBar(levelValue));
}
I’m getting this error:
NullReferenceException: Object reference not set to an instance of an object
Script_ClickToLoadAsync.ClickAsync (System.String levelName) (at Assets/scripts/Script_ClickToLoadAsync.cs:19)
Line 19 is this line:
int levelValue = (int)manager.GetType().GetField(levelName).GetValue(manager);
Could someone please explain to me what I’m doing wrong and how to solve this?
Thank you in advance for your assistance.