Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Pikarie · Aug 26, 2020 at 06:26 PM · buttonclickscene-change

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? alt text

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
         }
     }
 }

2020-08-26-14h02-36.gif (358.1 kB)
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image highpockets · Aug 27, 2020 at 12:11 AM 0
Share

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.

avatar image Pikarie highpockets · Aug 27, 2020 at 12:17 AM 0
Share

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.

alt text

alt text

2020-08-26-20h16-23.png (6.4 kB)
2020-08-26-20h16-39.png (5.8 kB)
avatar image Pikarie highpockets · Aug 27, 2020 at 12:20 AM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

239 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Scene Buttons not working after scene switch 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges