Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 MidievalMan · Aug 12, 2020 at 02:16 PM · uibuttonbuttonsbutton trigger eventsonclick

UI Button OnClick Only Works the First Time

Hello, I'm working in a game that has levels. Since each level is simply a unique wave of enemies, I have all the levels take place in the same scene. I control which level should be happening using a bool that becomes true when I click the button on the menu for that specific level. When I go from my level select menu (a separate scene) to my scene where the waves of enemies should spawn the first time, the bool becomes true and everything works, but the second time when I click the same button it still switches scenes, just the bool doesn't become true, so no enemies spawn or anything.

I've been struggling with this for about 2 hours now and I really have no idea why it's happening. The really confusing part for me is why the button calls both the function to make the bool true and the function that switches scenes the first time, but only the function that switches scenes the second time. If you need code I can give some, but pretty much how it's set up is I have a singleton between the two scenes that has both function to make the level bool true and the other function in the same script to make the bool false when all enemies are defeated.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by lvskiprof · Aug 12, 2020 at 04:23 PM

What is the trigger that causes it to call the function that sets the bool to true? It sounds like when you switch scenes you need to reset that trigger back to its original state and then it will call your function.

Hard to be sure without seeing the code, but that seems to fit what you described.

Comment
Add comment · Show 2 · Share
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 MidievalMan · Aug 13, 2020 at 07:45 PM 0
Share

Also, I just now realized that none of the buttons work the second time around if that helps anyone with solving this problem.

avatar image MidievalMan · Aug 13, 2020 at 08:02 PM 0
Share

Ah, I've found the problem. Turns out many others have had the same problem. The buttons are missing the reference to my singleton because the link is broken when I enter the scene and come back. It looks like I have to create a script that will stay in the scene and have my Wave$$anonymous$$anager script reference that.

avatar image
0

Answer by MidievalMan · Aug 13, 2020 at 06:14 PM

The trigger is the built in "On Click ()" thing that shows in the inspector when you click on a button in the editor. I dragged in my persistent singleton manager script to that and chose this function from it:

     public void L3T()
     {
         L3 = true;
         ResetTimer();
     }


The reset timer function just sets a timer i use to know when to spawn enemies back to 0. Also, at the end of the level I run this function which is also in my persistent singleton manager script:

     public void EndLevel()
     {
         turretStats.SaveTurretStats();
         GameManagerScript.Instance.hasControl = false;
         Time.timeScale = 0;
         GameManagerScript.Instance.winMenu.SetActive(true);
         GameManagerScript.Instance.enemiesKilledText.text = "Enemies Killed: " + GameManagerScript.Instance.enemiesKilled.ToString();
         L1 = false;
         L2 = false;
         L3 = false;
         L4 = false;
         L5 = false;
         L6 = false;
         L7 = false;
         L8 = false;
         L9 = false;
         L10 = false;
     }

What I see happening in the inspector is that L3 (the bool controlling whether it is level three) starts out as being false. Then, when I click the button to enter level 3, I see it become true in the inspector. Then, at the end of the level it becomes false as expected because I run the function above after all the enemies in that level are destroyed. Finally, it remains false when I click the button to enter level 3 again. Hope this code helps, I'll keep troubleshooting. By the way, I've tested this on some of my other levels just to make sure it's not just level 3 and the rest of them work the same way.

Comment
Add comment · Share
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

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

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

How can I generate and setup buttons via code 1 Answer

is it possible to have two on click events in one button 1 Answer

How do I change a state in an FSM with an UI button? 0 Answers

AddListener not working after build 0 Answers

Dymanic buttons with different openURL links 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