0
\$\begingroup\$

Develop a timer script, the idea of this script is that after an amount of time has passed, a button that I have in the scene is activated, this script also uses this script uses DontDestroyOnLoad, but I cannot activate it because of the following error:

"MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. DigitalRubyShared.Timer.Update () (at Assets / DIC / Scripts / Timer.cs: 53)) "

From what I understood is that this button object is destroyed when I change scenes, so how can I keep this object?

 public static float currentTime;
        public static float endTime = 0f;
        public static bool startTime = false;
        public static string recibir;
        public static Timer estadoTimer;
        public string tiempoEscena;
        public string nombreEscena = "Mapa_Izquierdo";

        public GameObject aceptar;
        [SerializeField] Text Contador;

        void Start()
        {
            currentTime = 0f;
        }

        void Awake()
        {
            if(estadoTimer == null)
            {
                estadoTimer = this;
                DontDestroyOnLoad(gameObject);
            }else if(estadoTimer != this)
            {
                Destroy(gameObject);
            }

        }

        void Update()
        {

            if (startTime != false)
            {
                currentTime += 1 * Time.deltaTime;
                Contador.text = currentTime.ToString("0");

                if (Contador.text.Equals(tiempoEscena))
                {
                    //SceneManager.LoadScene(nombreEscena);
                    aceptar.SetActive(true);
                    currentTime = endTime;
                    stopTiempo();
                }

            }
            else
            {
                Contador.text = currentTime.ToString("0");
            }

    }
\$\endgroup\$
0
\$\begingroup\$

Its looks like this problem well only occur when you reload the scene after first time? if i am correct please let me know about it.

Reason:

As you have made this class as a singleton class and set it to "DoNotDestroy", that's the reason which generates the error. Basically what happen when you load this scene for the first time this game object is set to Do not destroy and for the class it is set up to singleton so when you reload the scene at the second time or after that this

if(estadoTimer == null)
            {
                estadoTimer = this;
                DontDestroyOnLoad(gameObject);
            }else if(estadoTimer != this)
            {
                Destroy(gameObject);
            }

will destroy destroy the game object . i know I have not presented the problem reason really well but i tried. But solution will work :).

Solution:

When ever you are trying to disable the button before that you check for it if the button is available, if it is available then just enable or disable it.

if the button is null then first Find the button with tag or name but i prefer to use Tag instead of name, here is how i do it:

Finding object with Tag: More information

GameObject.FindGameObjectsWithTag("YourButtonTag");

Finding by Name: More information

GameObject.Find("YourButtonName");

Your Code will become some thing like this:

Note: Before you do this add Tag to the button. Any thing you want.

public static float currentTime;
public static float endTime = 0f;
public static bool startTime = false;
public static string recibir;
public static Timer estadoTimer;
public string tiempoEscena;
public string nombreEscena = "Mapa_Izquierdo";

public GameObject aceptar;
[SerializeField] Text Contador;

void Start()
{
    currentTime = 0f;
}

void Awake()
{
    if(estadoTimer == null)
    {
        estadoTimer = this;
        DontDestroyOnLoad(gameObject);
    }else if(estadoTimer != this)
    {
        Destroy(gameObject);
    }

}

void Update()
{

    if (startTime != false)
    {
        currentTime += 1 * Time.deltaTime;
        Contador.text = currentTime.ToString("0");

        if (Contador.text.Equals(tiempoEscena))
        {
            //SceneManager.LoadScene(nombreEscena);
            if(aceptar!=null)
            {
                   aceptar.SetActive(true);
            }
            else 
            {
                   aceptar =  GameObject.FindGameObjectWithTag("Your Button Tag Here");
                   aceptar.SetActive(true);
             }
            currentTime = endTime;
            stopTiempo();
        }

    }
    else
    {
        Contador.text = currentTime.ToString("0");
    }

}

I hope this will help...

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.