I’ve been looking for weeks for a simple script to trigger an animation on the player using collision but can’t seem to find any info.
I’ve been trying this one I found, but it tells me “the animation state ‘bench sit’ could not be played because it couldn’t be found!”
function OnTriggerEnter (collision : Collider) {
if (collision.gameObject.tag == “Player”){
collision.GetComponent.().Play (“bench sit”);
// we want the player’s animation to play, not the trigger’s.
}
}
I’ve tried setting the animation to every setting from ‘none’ to ‘humaniod’ and in between.
I’ve looked at all of the threads where people are having similar problems but none of the fixes work.
Does anyone have a simple script for this they might be willing to share? Or maybe help me figure out why it isn’t finding the animation from the script above?
I have another script that uses key buttons to access the player animations and the animations work when used with that. It’s just the triggering on collision I’m not able to do.
I have the script attached to the trigger object. I had it attached to the player also but when that didn’t work I removed it from the player.
I’m really new to scripting and I’m totally lost. Please help.
Thanks in advance.
Your code seems correct. It may eventually come from the space between bench and sit. Try to setup your animation without space (like bech_sit for example).
You need also to check if in the Animation (using Inspector) you can see your bench sit clip.Provide screenshots also.
Game Rules
Unified Visual Scripting Asset with rules engine
Hey. Thanks for the reply. I appreciate it. I tried renaming it without the space but still get the same error message. What should I take a screenshot of? The inspector for the trigger object?
Writing this from my phone but does this work?
function OnTriggerEnter (Collider collider) {
if (collider.tag == "Player"){
collider.GetComponent.<Animation>().Play ("bench sit");
}
Or maybe try:
private void OnTriggerEnter (Collider collider) {
Instead of
function OnTriggerEnter (Collider collider) {
Hi. Thanks for the response. 
When I tried that code I get this error :
Assets/animationtrigtest.js(1,35): BCE0043: Unexpected token: collider.
You are getting a collider from the trigger function, not the gameObject.
you checked for the player tag, but you are trying to get the animation from a component of the collider, not the gameObject.
It should be collision.gameObject.getComponent().Play(“”);
Thanks for the response. Ok so this is what I currently have:
function OnTriggerEnter (collision : Collider) {
if (collision.gameObject.tag == “Player”){
collision.gameObject.getComponent().Play(“bench_sit”);
}
}
But I’m getting the following errors:
Assets/animationtrigtest.js(6,46): BCE0043: Unexpected token: ).
Assets/animationtrigtest.js(6,47): BCE0044: expecting ), found ‘.’.
Assets/animationtrigtest.js(6,48): UCE0001: ‘;’ expected. Insert a semicolon at the end.
Any idea what’s causing those? I don’t know how to fix these cause there is a semicolon at the end of line 6 already ,but it’s asking for one.
It’s basically impossible without seeing the lined code. It’s definitely a syntax error. Sometimes the line isn’t right. It might be something not named properly, or that should have been in quotes but wasn’t, or you didn’t include a close parenthesis, These are all 46, 47, and 48 characters over in the line which should give you an idea of what’s going wrong, but that’s where it found something, probably all right, but something previous gave it a false idea of the code. Always check your brackets and parenthesis and make sure there isn’t one missing or too many.
I’m not sure what you mean by lined code , I’m a scripting newb (but you could probably tell that) . What I have up there is the entire script that I’m using.
All of the errors are coming from line 6 apparently.
Like I said I’m a newb and I’m not sure what it means when it references numbers like 46,47,48 when I only have like 7 lines of code.
I know this is probably frustrating, but I really do appreciate you taking your time out to help me. I’m just learning as I go along. But I appreciate any help you can give.
You don’t have the class name, etc. Is that the only function in your script? Are you using c# or javascript? If that’s all your code, you are using javascript.
Ok, yeah, you are using javascript, which I would seriously think about because most tutorials use c#. Anyway, here is the code:
#pragma strict
function OnTriggerEnter (collision : Collider) {
if (collision.gameObject.tag == "Player"){
collision.gameObject.GetComponent.<Animation>();
}
}
If you look closely, there is a period after GetComponent in javascript which isn’t in c#. You also didn’t have GetComponent capitalized in your code. Code completion should be helping you with those types of errors. I didn’t add the Play function, but you should be able to do that.
Thanks I appreciate the help. That fixed the errors, but it’s still telling me “The animation state could not be played because it couldn’t be found”.
Any idea what I might be doing wrong there? I have the animation clip attached to the player and I know it works because I have it set up where it can be used by hitting a key on the keyboard.