Game Object disappears from inspector when I press play

I’ve added a mechanic into my game so that when the player collides with a trigger a button will appear for them to click, and once they do a player animation will activate.

Here’s the script for that:

{
    [SerializeField]
    public Button enterButton;

    public bool enterAllowed;

    public Animator anim;
    
private void Start()
{
    enterButton.gameObject.SetActive(false);
    anim = GetComponent<Animator>();
}

private void Update()
{
    
}

private void OnTriggerEnter2D(Collider2D collision)
{
    if (collision.gameObject.name.Equals("Temp Player"))
    {
        enterButton.gameObject.SetActive(true);
        enterAllowed = true;
    }
}

private void OnTriggerExit2D(Collider2D collision)
{
    if (collision.gameObject.name.Equals("Temp Player"))
    {
        enterButton.gameObject.SetActive(false);
        enterAllowed = false;
    }
}

public void Replenish()
{
        anim.SetTrigger("isDrinking");
        //Debug.Log("Player is drinking");
    }
}

The script above is placed on the trigger which connects to the button on-click component. In it’s inspector in Unity, I have the character’s animator plugged in to the anim slot. The only problem present is when I play the game to test the mechanic, the anim slot automatically clears itself of the player’s animator component.

168999-example-pic.png

169000-example-pic2.png

Is there any way to fix this issue without having to replace the player’s animator component whenever playing? I’m sorry if I lacked necessary details on my issue. If you need any questions answered, I’d be happy to oblige.

In your screenshots, we cannot see the animator component.

As you already set it via the Editor, there is no need to reset it in the Start() function.

From what you describe, I would guess, that the animator is not a component of the game object that has the script attached.

Debug the line in your Start() function, and you’ll see.