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 Turkeybag · Sep 19, 2013 at 03:16 AM · functionyieldsendmessageyield waitforseconds

Calling a function in another script not working with yield

Alright so upon finding out sendmessage can only send 1 parameter I found an alternative:

 InstantForce = Target.GetComponent("CharacterMovement");
 InstantForce.InstantForce(100.0, ExplosionDirection);

I want it to pretty much start the function "InstantForce" in my CharacterMovement script. It does this although my old yield in a for loop trick doesn't work.

 function InstantForce (Length : float, ApplyForce : Vector3)
 {
     for (var A = Length; A > 0; A--)
     {
         rigidbody.AddForce(ApplyForce*(A/Length), ForceMode.Impulse);
         yield new WaitForFixedUpdate();
         Debug.Log(A);
     }
 }

It dubugs 100 once when I move the debug above the yield but comes up blank while it's there. I think it's something to do with the way I'm calling it? If so what's the fix? I'm clueless!

EDIT: This works if I call it instead of InstantForce but it just seems so pointless! There has to be another way?

 function ApplyInstantForce (Length : float, ApplyForce : Vector3)
 {
     InstantForce(Length, ApplyForce);
 }
Comment
Add comment · Show 1
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 ArkaneX · Sep 19, 2013 at 09:49 AM 0
Share

Just FYI - I did a quick test with two scripts attached to two objects. I used two first lines from your code inside Start method of the first script, while InstantForce method was in the second. Everything worked as expected.

1 Reply

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

Answer by oliver-jones · Sep 19, 2013 at 10:43 AM

This is more of a side note, than an answer.

You mentioned that SendMessage can only send one parameter. Well, there is a work around, you could send an array:

    var playerInfo = new Array();
 
     //Sender
     playerInfo.Add(10);
     playerInfo.Add("Tim");
     playerInfo.Add(100);
     
     SendMessage(PlayerInfo, paraArray);
 
    //Receiver
     function PlayerInfo(info : Object){
     
        print("Player Damage: " + info[0]);
        print("Player Name: " + info[1]);
        print("Player Health: " + info[2]);
     
     }
Comment
Add comment · Show 5 · 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 Turkeybag · Sep 19, 2013 at 01:22 PM 0
Share

So long as it works I'm happy. Better than my way anyway. However! The question is, can a single array contain a bunch of different things? So an array with a vector3, float, and so on? Docs don't really tell me and I'm pretty bad at arrays :S

avatar image oliver-jones · Sep 19, 2013 at 01:24 PM 0
Share

If you use 'new Array' then you can define anything in it. Like my example, it's using both Strings and Ints. So yea - you can!

new Array will hold Objects. And an Object are all the GameObjects, Transforms, Vector3, Strings ... etc

avatar image ArkaneX · Sep 19, 2013 at 01:42 PM 0
Share

@Turkeybag - if you're going pass more than one parameter in one object, don't use Array. Just write a separate class and store your data inside, e.g.

 class InstantForceData
 {
     var length : float;
     var force : Vector3;
 
     function InstantForceData(l : float, f : Vector3)
     {
         length = l;
         force = f;
     }
 }
avatar image Turkeybag · Sep 19, 2013 at 02:38 PM 0
Share

Didn't even know about that class thing. I'll have to have a proper mess around with it and arrays. Should fix my problem. Thanks!

avatar image fafase · Sep 19, 2013 at 03:01 PM 0
Share

And what if I pass only two variables in my array? Your method crashes since it expects 3 values. You could still use a for loop but that does not sounds real great. The class thing is the solution.

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

Coroutoutine doesnt work properly. 3 Answers

StartCoroutine important for using yield? 1 Answer

How to use yield within a class function 2 Answers

Update() can not be a coroutine. 2 Answers

Custom Yield Instruction for FixedUpdate frames 3 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