- Home /
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();
}
}
}
}
}
You have to play the audio somewhere. Like this: bellsring.Play();
Check this out https://docs.unity3d.com/ScriptReference/AudioSource.Play.html
Also copy/paste the full error log here, maybe I can help you with that
thanks that fixed the sound issue
error 1 Animator.GotoState: State could not be found UnityEngine.Animator:Play (string)
error2 Invalid Layer Index '-1' UnityEngine.Animator:Play (string)
Answer by prgramernoob101 · May 22 at 07:46 PM
Here's a quick way to debug your code.
Click the error bar at the bottom
Then inside the console window that popped up you can select the error/warning/message and see more details
You can see that the warning is coming from line 9 on TestingScript.cs
You can also click on the blue text and it will open in your code editor with the problematic line selected.
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.
Your answer
Follow this Question
Related Questions
How to setup gun shot sound using only animator 0 Answers
2D Animation does not start 1 Answer
How to make more than one animation with the animator? 0 Answers
Animation not playing! Help needed! 1 Answer
Animation Keypress Trigger Problem 0 Answers