Need help Script error, i got 4 errors here is my script
// C# code
void OnTriggerEnter(Collider Obj) { if(Obj.tag == "Enemy") { gameobject.Getcomponent().Play("ATTACK"); } }
Below are the errors
Assets/Swing.js(3,7): UCE0001: ';' expected. Insert a semicolon at the end. Assets/Swing.js(3,32): BCE0044: expecting ), found 'Obj'. Assets/Swing.js(3,35): BCE0043: Unexpected token: ). Assets/Swing.js(7,46): BCE0043: Unexpected token: ).
Answer by metalted · Mar 05, 2017 at 08:13 PM
Please pay attention to how functions are written:
Its gameObject, not gameobject. Its GetComponent(), not Getcomponent(). The code is case sensitive so you have to pay attention to that in the future.
Getcomponent().Play() doesnt really make sense, what component are you calling ? You probably want to play a sound called ATTACK? In that case you need to get the AudioSource component to play the audioclip.
Your code has to be rewritten into this:
public AudioClip attackSound;
void OnTriggerEnter(Collider Obj)
{
if(Obj.gameObject.tag == "Enemy")
{
gameObject.GetComponent<AudioSource>().Play(attack);
}
}
In some cases you can leave the "gameObject" out of it. I just like to write it most of the time, because it makes the code more readable for me. For example:
gameObject.GetComponent<AudioSource>()
could be written as:
GetComponent<AudioSource>()
They both get the Audio Source of the GameObject this script is attached to.
All i want is to play my tower attack animation when enemy step on the box trigger i put..
Your answer
Follow this Question
Related Questions
How can I detect what animation condition an animator is in through scripting? 0 Answers
How can i stop my shooting animation after playing? 0 Answers
ANIMATION STRANGELY DOES NOT PLAY 0 Answers
2020.1.15f1 How to set animations "LoopTime" to false from script 2 Answers
Walking animation javascript? 2 Answers