- Home /
Reloading scene from button only works once
So I have an incredibly simple script attached to this button:
public class ResetButton : MonoBehaviour
{
public void ReloadLevel()
{
Debug.Log("Button Clicked");
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
And I am assigning this function to the onclick event in the editor. The first time I click the button, it works correctly. However the second time it seems like the button no longer has the correct object reference in the onclick, it does not even call the function, though I also do not get an null reference error. I am using 2019.4.8f1.
Any ideas why this doesn't work after the level has been reloaded?
Answer by Real_Person_ · Feb 11, 2021 at 08:54 PM
@Zaphodb2002 I had this same problem and sort of went in a huge circle in order to fix. However, now that I run your code it seems to work, interesting. I'm using Unity 2020, so it might be a bug. Potential causes?
In the inspector, is the on click assigned by dragged and dropping the object containing the desired script, and then that script selected on the right part of the component?
Does the button have an event system? (for an unrelated problem it also needed the standard input module to work)