- Home /
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?
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.
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.
Would you $$anonymous$$d, share your workaround to us?
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?
Is there an EventSystem on all the scenes you are using the Canvas? Canvas objects need EventSystem in order to work.
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);
}
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
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?
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.
Your answer
Follow this Question
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