Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by bayerly · Apr 17, 2012 at 02:23 AM · animationjavascripttriggerontriggerenter

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

Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bayerly · Apr 17, 2012 at 03:11 AM 0
Share

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.

avatar image IgnoranceIsBliss · Apr 17, 2012 at 03:21 AM 0
Share

What line are you getting that error on?

avatar image bayerly · Apr 17, 2012 at 03:24 AM 0
Share

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

avatar image IgnoranceIsBliss · Apr 17, 2012 at 03:35 AM 0
Share

Strange. $$anonymous$$y code was in C#, but there really shouldn't be much of a difference.

You would expect that error if...

  1. There is no animation with that name (check your casing)

  2. 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");

avatar image bayerly · Apr 17, 2012 at 04:00 AM 0
Share

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.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by IgnoranceIsBliss · Apr 17, 2012 at 02:58 AM

Camera.main.animations.Play("MyAnimationName");

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges