- Home /
Cueing an Animation From Another GameObject's Script (Javascript)
Hey everyone,
so I've got the basic "collect items for points" script below, and want to add an animation when the player hits the trigger. The animation is attached to the MainCamera object (and must be, as it modifies an ImageEffect), and I want to cue it from this script.
Script:
var score = 0;
var scoreText = "Score: 0";
var MyFont : Font;
function OnTriggerEnter( other : Collider ) {
Debug.Log("OnTriggerEnter() was called");
if (other.tag == "Coin") {
Debug.Log("Other object is a coin");
score += 5;
scoreText = "Score: " + score;
Debug.Log("Score is now " + score);
yield WaitForSeconds (.2);
Destroy(other.gameObject);
}
}
function OnGUI () {
GUI.skin.font = MyFont;
GUI.color = Color.black;
GUI.Label (Rect (10, 10, 500, 300), scoreText);
}
It's javascript, and I've tried a few options, but they seemed to mostly be in C#. Thanks for the help!
Pat
IgnoranceIsBliss: As I'm getting some errors saying "object reference not set to an instance of an object", I think I'll clarify that the script is attached to a GameObject that is called "Player", while the $$anonymous$$ain Camera is a child of that object, and possesses the Image Effects and the animation.
I inserted it on line 13 of the script, and the error is on the snippet you provided. I was pretty sure it would work, but functions below where the script was inserted stopped functioning as well. The only bit I changed was "$$anonymous$$yAnimationName" to the animation's name. Thanks a ton for all the help BTW
Strange. $$anonymous$$y code was in C#, but there really shouldn't be much of a difference.
You would expect that error if...
There is no animation with that name (check your casing)
There is no main camera. If that is true, perhaps it would be best to create a 'var $$anonymous$$ainCam : Camera' at the start of your behaviour and then drop the camera into the Inspector in Unity.
Then the code would change to $$anonymous$$ainCam.animations.Play("CameraAnimation");
I fiddled with it a bit, but maybe Javascript doesn't allow the syntax "Camera.main....etc." That's completely a guess, I'm not sure. Still getting the NullReferenceException error.
I tried the new changes and still co$$anonymous$$g up with the same error on line 13.
Answer by IgnoranceIsBliss · Apr 17, 2012 at 02:58 AM
Camera.main.animations.Play("MyAnimationName");
Answer by bayerly · Apr 17, 2012 at 06:59 AM
Hah! Got it working! Somehow it wasn't understanding the label I was giving, but since animation.Play() plays the default animation on the camera, I changed it to that and it's working! I'm sure I can't have it be the only animation for long, but for now I can work with this. Thanks so much!
Your answer
Follow this Question
Related Questions
Animator Trigger Not Working 1 Answer
Why isn't my animation being played? 0 Answers
How to delay animation after OnTriggerEnter?? 2 Answers
Check collision of childs Trigger in Parents script [JS] 1 Answer
I cant get my animation to trigger. 0 Answers