Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by studentvz · Aug 30, 2016 at 03:02 PM · uibuttonsoundonclick

Button OnClick() after Scene restart is Missing (Object)

Hello,

I have a GameObject with AudioSource and script which have this piece of code: void Awake () { if (!instance) { instance = this; } else { Destroy (this.gameObject); } DontDestroyOnLoad (this.gameObject); }

I have also other methods, and main purpouse of this gameobject is to hold clips and play it by calling methods. When I add this GameObject to my Button OnClick() it works fine, but if I restart scene then there is no that GameObject assigned to Button OnCLick(), it says: Missing (Object).

Why is this happening?

Comment
Add comment · Show 7
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 _Yash_ · Aug 31, 2016 at 07:33 AM 0
Share

Your way of creating singleton is correct, but when you load new scene it will create new object having this script and this new object will be assigned to button onClick, now when awake is called this new object is destroyed because instance will be refering to an old object.

avatar image studentvz _Yash_ · Aug 31, 2016 at 10:24 AM 2
Share

I understand. So, you can't use buttons OnClick() for any objects/scripts that are singleton. It's a bad situation. I solved it with a new script that it is not a singleton, and in this script I call methods from another (singleton) script. It works great.

avatar image mochadwi20 studentvz · May 16, 2017 at 05:39 PM 0
Share

Would you $$anonymous$$d, share your workaround to us?

avatar image ZiB_Plays · Apr 04, 2017 at 06:31 PM 0
Share

Hi @studentvz, I am getting the same issue. I'm using "DontDestroyOnLoad (transform.gameObject);" on a canvas. The canvas is basically a HUD, on this HUD are buttons with functions attached- mouse click sound + change scene etc. They work on the first scene but when switching scenes the buttons are missing functions and non clickable.

What was your solution?

avatar image arielsan ZiB_Plays · May 17, 2017 at 12:58 AM 0
Share

Is there an EventSystem on all the scenes you are using the Canvas? Canvas objects need EventSystem in order to work.

avatar image arielsan · May 17, 2017 at 12:55 AM 0
Share

Note: you can use the code snippet to add code to avoid losing its format and make it unreadable for others, for example:

 void Awake () { 
      if (!instance) { 
           instance = this; 
       } else { 
           Destroy (this.gameObject); 
       } 
       DontDestroyOnLoad (this.gameObject); 
 }


avatar image mochadwi20 arielsan · May 17, 2017 at 11:33 AM 0
Share

The singleton pattern might be safe, due to creating new object after reloading a scenes. (the previous one is missing its reference to the UI button)

You might wanna try to using this approaches ins$$anonymous$$d: saving object after reloading scene

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Ardaaytac · Aug 31, 2016 at 07:30 AM

Hi, I dont know the whole code but this part is okay. Did you tried adding listener to your buttons on OnEnable function?

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
avatar image
0

Answer by ZiB_Plays · May 20, 2017 at 09:19 AM

My solution was to copy paste my manager into every scene and link func.s to the buttons. Then just disable the manager via inspector tick box to avoid having 2 listeners error. Just keep the original manager in first scene enabled and carry that one across if you need to. Scripts still work on the diabled manager. Func. say linked.

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

7 People are following this question.

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

Related Questions

Move Button OnClick listeners to another button 3 Answers

List of Buttons: Adding a listener passes Last element? 2 Answers

How to access the OnClick event via Script 1 Answer

Button.onClick.AddListener inconsistent 0 Answers

How to ADD one Buttons.onClick events to another Buttons.onClick? 1 Answer


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