static MusicPlayer myMusic = null;
// Use this for initialization
void Start ()
{
if (myMusic != null)
{
Destroy (gameObject);
print (“Destroyed instance”);
}
else
{
myMusic = this; // every single myMusic will now have “This value” so it will never be called this far in code again. As a result, any new object goes poof
GameObject.DontDestroyOnLoad (gameObject);
}
}
This is my code and the buttons are not working for some reason, really stressful! Everytime i can use any code and my buttons just wont work and they are hooked up fine.
THANK YOU!
Hi Mary,
You can encapsulate the code between ``` code ``` (3 x backquotes; can find it on UK keyboards left of the 1 key and below Esc), so it would show up correctly formatted. By what I see, I don’t know, if the problem is with your code, or rather something else.
You can try this code (known working code) and if your issue still persist, then the code is probably not to blame.
using System.Collections.Generic;
using UnityEngine;
public class MusicControllerSingleton : MonoBehaviour {
static MusicControllerSingleton instance = null;
// Use this for initialization
void Awake () {
MakeSingleton();
}
void MakeSingleton () {
if (instance == null) {
instance = this;
DontDestroyOnLoad(gameObject);
} else {
Destroy(gameObject);
}
}
}
(Replace “MusicControllerSingleton” with the name of your class in the above code.)
I would be much more interested of the hierarchy there. What children are there that we don’t see? Could you open the whole hierarchy? Also a good idea to check what your buttons are set to do.