Chetan Waghela
Chetan Waghela

Reputation: 137

Restart a script on button press C# + Unity

I have a gameobject called glow and I have attached a script to it spiralclock.

I want this script to restart again from initial on button press.

However, nothing is happening. I checked using Debug.Log() and the button is getting pressed.

I have used this line in the code,

GameObject.Find("glow").GetComponent<spiralclock>().enabled = false;
GameObject.Find("glow").GetComponent<spiralclock>().enabled = true;

with OnButtonPressed() and OnButtonReleased() functions.

A code like this

SceneManager.LoadScene(SceneManager.GetActiveScene().name); // loads current scene

is also not working to restart the scene from start.

Upvotes: 0

Views: 1242

Answers (1)

Gintas
Gintas

Reputation: 285

If by the definition of button, you're using Unity's UI button, you could make a method like:

public void ButtonNameClick()
{
  // your restart logic goes here
}

You could then subscribe to this method(aka - to make this method execute on a button press) either through Editor, via OnClick() or through code.

Upvotes: 1

Related Questions