How to prevent button click when changing scene?
On my main menu, when I click a button, it changes scene. I'm not sure why, when the new scene opens, the button on that scene is clicked. The test button of the 2nd scene is a "go to previous scene" button.
How can I prevent the 2nd button (the one on the other scene) to trigger its click event?
This is the code to change scene used to generate a scriptable object that I put on my button "test" to go back on the main menu scene.
using UnityEngine;
using System.Collections.Generic;
/// <summary>
/// From https://answers.unity.com/questions/1617291/how-i-can-open-a-previous-scene-with-button-back.html
/// Generate a scriptable object which can be added to a gameobject component to load scenes in different ways.
/// </summary>
/// <example>
/// Drag and drop this scriptable object on the "click" gameobject slot of a button and select one of the methods to execute for the button's "click" event.
/// </example>
[CreateAssetMenu(fileName = "SceneManager")]
public class SceneManagerCreator : ScriptableObject
{
private Stack<int> loadedLevels;
[System.NonSerialized]
private bool initialized;
private void Init()
{
loadedLevels = new Stack<int>();
initialized = true;
}
public UnityEngine.SceneManagement.Scene GetActiveScene()
{
Debug.Log("GetActiveScene");
return UnityEngine.SceneManagement.SceneManager.GetActiveScene();
}
public void LoadScene(int buildIndex)
{
Debug.Log($"LoadScene buildIndex = {buildIndex}");
if (!initialized) Init();
loadedLevels.Push(GetActiveScene().buildIndex);
UnityEngine.SceneManagement.SceneManager.LoadScene(buildIndex);
}
public void LoadScene(string sceneName)
{
Debug.Log($"LoadScene sceneName = {sceneName}");
if (!initialized) Init();
loadedLevels.Push(GetActiveScene().buildIndex);
UnityEngine.SceneManagement.SceneManager.LoadScene(sceneName);
}
public void LoadPreviousScene()
{
if (!initialized)
{
Debug.LogError("You haven't used the LoadScene functions of the scriptable object. Use them instead of the LoadScene functions of Unity's SceneManager.");
}
if (loadedLevels.Count > 0)
{
Debug.Log($"LoadPreviousScene loadedLevels.Count = {loadedLevels.Count}");
UnityEngine.SceneManagement.SceneManager.LoadScene(loadedLevels.Pop());
}
else
{
Debug.LogError("No previous scene loaded");
// If you want, you can call `Application.Quit()` instead
}
}
}
Are you using onClick() for both buttons?? Because onClick() should only fire when the button is released, so if it is fired on both of the buttons on the frame when the button was released, it doesn’t make a whole lot of sense.
Both button are using a click event link to the code from the Scriptable Object "Scene$$anonymous$$anagerCreator". First image is the "Story $$anonymous$$ode" button. Second image is the test button to go back to previous scene.
I'm not sure if it's related to the ScriptableObject? I dont really know much about them for now. The behavior only occurs when I first start the game, after that, other clicks are working correctly. hmm.
Your answer
Follow this Question
Related Questions
Button Click 1 Answer
What is the best way to use buttons? 1 Answer
Help with Randomized Sprite on GUI button click 0 Answers
Get keycode of pressed button 1 Answer