Im trying to make a button that loads the scenes I tell it to. Im attempting to do this but using
public void GotoScene(int scene) , how would I use the int in another public void? My code is below if anyone may need it.
public class ButtonSceneTransition : MonoBehaviour
{
public Animator transitionAnim;
public string sceneName;
public int sceneNumber;
public void GotoScene(int scene)
{
transitionAnim.SetTrigger("end");
Invoke("GotoLevelScene", 1.5f);
}
public void GotoLevelScene()
{
SceneManager.LoadScene(scene);
}
}
Hello.
You are confucing some things.
The variable “scene” esxists only inside GotoScene() method, so for GotoLevelScene() there isnt any variable called scene.
You should store the “scene” value to some variable that exists in all the class, in all the script, something like sceneNumber variable:
public class ButtonSceneTransition : MonoBehaviour
{
public Animator transitionAnim;
public string sceneName;
public int sceneNumber;
public void GotoScene(int scene)
{
transitionAnim.SetTrigger("end");
sceneNumber = scene;
Invoke("GotoLevelScene", 1.5f);
}
public void GotoLevelScene()
{
SceneManager.LoadScene(sceneNumber );
}
}
Then to call i from other script, you have to find this instance of the script, or make the method GotoScene static, so all scripts ButtonSceneTransition share the same function:
public class ButtonSceneTransition : MonoBehaviour
{
public Animator transitionAnim;
public string sceneName;
public int sceneNumber;
public void static GotoScene(int scene)
{
transitionAnim.SetTrigger("end");
sceneNumber = scene;
Invoke("GotoLevelScene", 1.5f);
}
public void GotoLevelScene()
{
SceneManager.LoadScene(sceneNumber );
}
}
so now, from any other script you just need to call the function like this (for example scene 3):
ButtonSceneTransition.GotoScene(3);
You should look some tutorials about basic scripting, references, find other scripts, etc… you will need a lot.
Bye!