Hello,
For some reason my code works in the Start function but doesn’t in another function.
The code I’m executing is as follows:
var rand = Random.Range(0, 2);
if(rand <= 0.5) {
GetComponent.<Rigidbody2D>().AddForce(new Vector2(50, 2));
} else {
GetComponent.<Rigidbody2D>().AddForce(new Vector2(-50, -2));
}
When in function Start(), this works perfectly and the force is added to it nicely.
However, I decided I wanted to have a delay on the force, and let my master script handle that instead of the instantiated object where this code is located.
The code in the master script:
var ball : GameObject;
function Start () {
Instantiate(ball,
mainCam.ScreenToWorldPoint(new Vector3(Screen.width / 2, Screen.height / 2, 1f)),
Quaternion.identity);
startGame();
}
function startGame() {
yield WaitForSeconds(3);
ball.GetComponent(BallControl).fireBall();
}
BallControl is what the script of the first block of code is called.
When the fireBall() function is called, the force doesn’t get applied.
I know for a fact that it is called, because when I put debug in it, it logs as expected.
Have I done something wrong? How would I fix this issue?
Thank you.