Public GameObject Reference Disappearing When Used

I have a referenced gameobject (starts disabled) in a C# script, but when I try to use it, the object reference in the editor goes from the object, to saying Missing.

There is nothing in the script that sets the removal, not anything called which would do this, so I am at a loss to explain it.

The script is a unique, use-once script to control the initial cutscene, so is quite specific.

The variable that is disabled, and disappears is “player”, and occurs when I try to enable it, on line 32.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameStartControl : MonoBehaviour
{
    public GameObject playerCryo;
    public GameObject player;
    public int secondsToStart;

    void Start()
    {
        StartCoroutine(StartGO());

    }

    IEnumerator StartGO()
    {
        FadeInOut[] fade = Resources.FindObjectsOfTypeAll(typeof(FadeInOut)) as FadeInOut[];

        yield return StartCoroutine(WaitFor.Seconds(secondsToStart));

        foreach (FadeInOut f in fade)
        {
            f.FadeOut(); Debug.Log(f.name);
        }

        yield return StartCoroutine(WaitFor.Seconds(3));

        playerCryo.SetActive(false);

        player.SetActive(true);

        foreach (FadeInOut f in fade)
        {
            f.FadeIn();
        }
    }
}

Might be obvious, but have you checked the hierarchy? Is the GameObject still there when the error occurs?

The object does disappear, should have mentioned that.
The issue is that it doesn’t happen when I disable that script, which means the script, for some reason, is causing this, despite having nothing coded which could.

I honestly can’t see how that script could cause that. Just to be sure, check any place where you’re destroying GameObjects.

Have you recently changed the hierarchy in the scene? In case the player being destroyed is a side-effect of destroying something else up in hierarchy.

The only place destroying objects would be the character controller, which destroy any active copies of itself, but as I am setting the Cryo version to false before setting the main one to true, that shouldn’t be a problem.

How are you doing this? I only ask because you’re using FindObjectsOfTypeAll in the code you posted which finds active and inactive objects (and is also obsolete btw).

@KelsoMRK Nope, I’m using FindObjectsOfType (the normal one), didn’t even know the All one existed until about a week ago :stuck_out_tongue: