- Home /
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);
}
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.
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]);
}
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
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
@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;
}
}
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!
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
Follow this Question
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