I have a script where a player comes across a torch (sprite), when he passes it lights up as a checkpoint. I created the animation, but I need to hide the animator until the player makes contact. I cannot get it to work, the animation always plays, any help would be appreciated, this is my first foray into controlling animation.
Thanks
-Paul-
here is the code:
using UnityEngine;
public class CheckpointAnimationController : MonoBehaviour
{
public SpriteRenderer idleImage;
public Animator activeCheckpoint;
public bool checkpointActive;
private SpriteRenderer spriteRenderer;
private Animator animatorRenderer;
void Start()
{
spriteRenderer = GetComponent();
animatorRenderer = GetComponent();
animatorRenderer.gameObject.SetActive(false);
}
private void OnTriggerEnter2D(Collider2D collider)
{
if (collider.tag == “Player”)
{
checkpointActive = true;
spriteRenderer.gameObject.SetActive(false);
animatorRenderer.gameObject.SetActive(true);
}
}
}
If I had to guess, you’re not disabling the correct game object.
void Start()
{
spriteRenderer = GetComponent<SpriteRenderer>();
animatorRenderer = GetComponent<Animator>();
// This animator renderer is connected to the same game object this
// component is attached to. Are you sure you're intending to disable
// this game object?
animatorRenderer.gameObject.SetActive(false);
}
If that’s not the case I think I would need more of a context as to how you have your objects arranged in the scene to be able to help.
The way I have this setup is I created an empty game object, and tied the script to that, then put both the sprite and animation under that. And brought them into the inspector, the sprite and the animation work. I just can’t control the animator for some reason.
I don’t see a renderer on the Animator, unless I’m not looking correctly.
I’m positive others have done this, maybe take a different approach, I like to keep the scripts single purpose and not call other objects unless I absolutely have too. Makes for neater and easier to control code.
Thank You