- Home /
Why is this script not working? (SendMessage)
I have two scripts. One on a wall, another on a ball. I'm trying to access a void from one script, in a different script.
Here is the script with the function:
public void ResetBall ()
{
var rigY = rigidbody2D.velocity.y;
var rigX = rigidbody2D.velocity.x;
var traY = transform.position.y;
var traX = transform.position.x;
rigY = 0f;
rigX = 0f;
traY = 0f;
traX = 0f;
Invoke("goBall", 0.5f);
}
And here is the script trying to access it:
void OnTriggerEnter2D (Collider2D hitInfo)
{
if (hitInfo.name == "Ball")
{
var wallName = transform.name;
gameMan.Score (wallName);
hitInfo.gameObject.SendMessage ("ResetBall");
}
}
Not going to lie this is all pretty advanced for me, but according to the video i'm watching (who is using javascript), the hitInfo should be able to send a message into the ball, because the ball IS "hitInfo" technically.
All I end up getting are warnings saying that rigy/x, and tray/x are not being used. And the ResetBall function doesn't go through.
Answer by SilentSin · Sep 26, 2014 at 05:23 AM
var rigY = rigidbody2D.velocity.y;
and
rigY = 0f;
Doesn't do what you think it does.
You're getting a copy of the velocity value, then setting the copy to 0. But the velocity isn't affected by the copy. You need to set the velocity directly:
rigidbody2D.velocity = new Vector2(0, 0);// or "= Vector2.zero";
The same applies to transform.position (using Vector3 instead of Vector2).