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 Rick74 · Nov 27, 2013 at 12:29 PM · javascriptdelegates

UnityScript, Delegates and SmoothMoves

Still learning code on my own here and I'm trying to understand delegates and how they would work in SmoothMoves.

edit Just to expand on this because this is the issue I'm having;

I'm trying to work with SmoothMoves while using UnityScript (JS). To do so, I need to work with delegates.

this is the code have, while it doesn't cough out any errors, it doesn't do what I 'think' it should.

 #pragma strict
 
 import SmoothMoves;
 
 var hitPoints                     : int     = 20;
 var cost                        : int     = 100;
 
 var tilePartner                  : GameObject;
 var tomatoSlot                    : GameObject;
 
 var placementAudio                : AudioClip;
 var myAnimation                    : SmoothMoves.BoneAnimation;
 
 private var projectileStrength    : int      = 0;
 private var soundRate             : float    = 0.0;                    
 private var soundDelay            : float = 0.0;
 var isAttacking                    : boolean = false;
 var isSpawning                    : boolean = true;
 var isIdle                        : boolean = false;
 
 function Start ()
 {
     PlaySound ( placementAudio, 0 );
 }    
 
 
 function Update ()
 {
     if ( isSpawning )
     {
         Spawn ();
     }
     if ( hitPoints <= 0 )
     {
         tilePartner.tag = "placementPlane_Open";
         Destroy ( gameObject );
     }
     if (isIdle && !isSpawning && !isAttacking)
     {
         animation.Play("Idle");
     }
     if (isAttacking && !isSpawning)
     {
         myAnimation.RegisterUserTriggerDelegate(Throw);
     }
 }
 
 function Spawn ()
 {
     animation.Play("Spawn");
     yield WaitForSeconds (.8);
     isSpawning = false;
     isIdle = true;
 }
 
 function OnTriggerEnter ( other : Collider )
 {
     if (other.gameObject.tag == "alienAttack")
     {
         projectileStrength = other.GetComponent(alienAttack).strength;
         hitPoints -= projectileStrength;
         projectileStrength = 0;
     }
 }
 
 function PlaySound (soundName : AudioClip, soundDelay : float)        // calls to play sound based on sound file name attached to object
 {
     if ( !audio.isPlaying && Time.time > soundRate )                // sets sound rate
     {
         soundRate = Time.time + soundDelay;                            // option to add a delay to the sound before it starts playing
         audio.clip = soundName;
         audio.Play ();
         yield WaitForSeconds ( audio.clip.length );
     }    
 }
 
 function Throw(userTriggerEvent : SmoothMoves.UserTriggerEvent)
 {
     animation.Play("Throw");
     if (userTriggerEvent.frame == 13)
     {
         print ("throw");
     }
 }

My issue is this part right here in the function update;

 if (isAttacking && !isSpawning)
         {
             myAnimation.RegisterUserTriggerDelegate(Throw);
         }

The code will enter this section just fine, however...it's not calling the function "Throw" which I was assuming it should. It doesn't enter the function Throw at all. It just does nothing.

Being new to delegates, I'm unsure what I'm suppose to do to properly call the Throw function?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by raimon.massanet · Nov 27, 2013 at 02:51 PM

I have never used SmoothMoves, so I can't tell you how it works. However, I can tell you that when you register a delegate, you don't trigger a function, you just setup a function which will be called when an event is triggered.

So, if you write:

 myAnimation.RegisterUserTriggerDelegate(Throw);


you should not expect your function to be called there. Instead, you are setting up your function Throw to be called when a SmoothMoves.UserTriggerEvent happens. When will that event be triggered? That depends on the backend, so you should search for any documentation that describes when that particular event is triggered. Since your function is not being called, I would say that event is not being triggered at all.

In addition, you usually only need to register a delegate once. That means that every time that event happens, your function will be called. I see you are registering your delegate inside the Update function, and since you are never de-registering it, I don't think it is necessary.

Comment
Add comment · Show 2 · 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 Rick74 · Nov 27, 2013 at 03:01 PM 0
Share

Holy shit, THAN$$anonymous$$ YOU!!!

I completely understand now, thank you so much for this. It just made my lightbulb go off and gave me clarity!

Thank you, thank you, thank you!

$$anonymous$$ade 2 line changes and she now works like a charm!

avatar image raimon.massanet · Nov 27, 2013 at 03:06 PM 0
Share

I am glad I helped you :-)

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

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

JS Delegates 1 Answer

c# delegates/events using from js? 1 Answer

Difference between Function and function() Types 1 Answer


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