- Home /
Why does my code work in Start but not in another function?
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.
Answer by Laekh · Jun 27, 2015 at 10:01 PM
I've figured it out.
I think the reason it wouldn't work was that it didn't really know what to apply the force to. Instead of just using GetComponent, I've made it look for the ball.
Working code:
function FireBall() {
var rand = Random.Range(0, 2);
if(rand <= 0.5) {
GameObject.FindGameObjectWithTag("Ball").GetComponent.<Rigidbody2D>().AddForce(new Vector2(50, 2));
} else {
GameObject.FindGameObjectWithTag("Ball").GetComponent.<Rigidbody2D>().AddForce(new Vector2(-50, -2));
}
}
Of course this will work, but using FindGameObjectWithTag is actually not necessary.
When you call Instantiate you create a clone of the prefab you passed as source. In your original script you then use GetComponent on the prefab and execute your "fireBall" function on the prefab and not on your instantiated object.
Instantiate returns a reference to the newly created object. That's the one you want to use ins$$anonymous$$d.
var ball : GameObject;
private var instance : GameObject;
function Start ()
{
instance = Instantiate(ball,
mainCam.ScreenToWorldPoint(new Vector3(Screen.width / 2, Screen.height / 2, 1f)),
Quaternion.identity);
startGame();
}
function startGame() {
yield WaitForSeconds(3);
instance.GetComponent(BallControl).fireBall();
}
Answer by Hexer · Jun 27, 2015 at 08:46 PM
You haven't invoke the function that applies the force, in your second script. You have only told the script to get the script object and nothing else.
Add something like this, in the "startGame" function.
BallControl.Invoke("NameOfFunction",0.1f);
I mainly use C# but this should work, I have done JS before. Don't forget to change NameOfFunction to the desired function name. In this case it would be the function where your force script logic is put.
Thank you, but this doesn't seem to do the trick for me. The weird thing is, it's all called, but the force just doesn't get applied for some reason.
Code:
ball.GetComponent(BallControl).Invoke("FireBall", 3);
Code it calls after the 3 second delay:
function FireBall() {
var rand = Random.Range(0, 2);
if(rand <= 0.5) {
Debug.Log("Fired to the left");
GetComponent.<Rigidbody2D>().AddForce(new Vector2(50, 2));
} else {
Debug.Log("Fired to the right");
GetComponent.<Rigidbody2D>().AddForce(new Vector2(-50, -2));
}
}
It prints the debug but doesn't apply the force, it however does apply the force whenever the same exact code is called from the Start function.
Your answer
Follow this Question
Related Questions
Start() is called 2 times when Instantiating prefab 2 Answers
2D Animation does not start 1 Answer
Initialising List array for use in a custom Editor 1 Answer
Where to start? 3 Answers