- Home /
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.
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.
You set the anim in the Start(). So every time you start the game the anim is set by anim = GetComponent<Animator>();
Anything you have preset in the inspector will be overwritten (and clearly no Animator component is found in the the same gameobject that the Enter scripts is attached to)
Thank you so much for answering!
The reason there's no Animator component on the trigger (which is what the Enter script is attached to) is because I'm trying to access the player's animation component instead. I didn't think adding an Animator component to the trigger would benefit anything.
How do you suggest I go about fixing the issue? I'm still new to program$$anonymous$$g so I'm not sure how to put your advice to good use.
Just take out that line anim = GetComponent<Animator>();
Reason is that you don't need to set the animator in the script because A) you have already drag and dropped the Player animator to the Enter script in the Inspector B) it wouldn't work like that anyway since GetComponent<Animator>();
tries to find the component in the same gameobject that the script is in.
Answer by l3mon · Oct 11, 2020 at 06:39 PM
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.
Oh my goodness, thank you so much! It works perfectly now.
Your answer
Follow this Question
Related Questions
When I build the project gameObjects disappear 0 Answers
2D Gameobject Location and scaling in different screen size(mobile) 0 Answers
2D C# destroy a GameObject on collision 2 Answers
Draw Mode 0 Answers
All the objects in my script choose the same target. 2 Answers