ringing a bell script sound and animation issue

I have script (down bellow) that activates a bell then plays it animation, audio once, and use use a ray cast

the problem is the sound wont play (no errors)

the animation plays but gets 2 warring (Animator.GotoState: State could not be found) (invalid layer index -1)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
    
    public class Ringbell : MonoBehaviour
    {
        public float range = 100f;
        public bool IsRing = false;
        public GameObject bell;
        public GameObject Ghost;
        public AudioSource bellsring;
        public Camera fpsCam;
        private Animator animator; 
    
        void Update()
        {
            if (Input.GetMouseButton(0))
            {
                if ((IsRing == false))
                {
                    StartCoroutine(ringthebell());
                }
            }
        }
    
        IEnumerator ringthebell()
        {
            bell.SetActive(true);
            IsRing = true;
            bell.GetComponent<Animator>().Play("ringbell");
            yield return new WaitForSeconds(1f);
            bell.GetComponent<Animator>().Play("none");
            IsRing = false;
            bell.SetActive(false);
    
            RaycastHit hit;
            if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
            {
                Debug.Log(hit.transform.name);
    
                Dissapper dissapper = hit.transform.GetComponent<Dissapper>();
                if (dissapper != null)
                {
                    dissapper.Gone();
                }
    
                if ((Ghost == true))
                {
                    EndGame endgame = hit.transform.GetComponent<EndGame>();
                    if (endgame != null)
                    {
                        endgame.Die();
                    }
                }
    
            }
        }
    }

Here’s a quick way to debug your code.

  1. Click the error bar at the bottom
  2. Then inside the console window that popped up you can select the error/warning/message and see more details
  3. You can see that the warning is coming from line 9 on TestingScript.cs
  4. You can also click on the blue text and it will open in your code editor with the problematic line selected.

196652-image-2022-05-22-123323168.png


I think the reason why you are getting a warning is because you’re trying to play a state that does not exist in your animator.


Below there’s two states “Default” and “Ringing” I can only play these from my script.
Make sure your code and animator state names match.