Update instatiate doesn't work?
I have 4 empty game objects from which to launch balls ( like Crash Bash Ballistix )but after the first ball is launched ( Start ) nothing happens. It works if i attach 4 scripts to these objects but that's lame and I want it all in one while attached to some other object like arena.
public Rigidbody Ball;
private float Timer = 0;
private Rigidbody newBall = null;
public GameObject Launch1;
public GameObject Launch2;
public GameObject Launch3;
public GameObject Launch4;
void Start () {
newBall = Instantiate(Ball, Launch1.transform.position, transform.rotation) as Rigidbody;
newBall.AddForce (new Vector3 (-220, 0, -150));
}
void Update()
{
Timer += Time.deltaTime;
if (Timer == 40) {
newBall = Instantiate (Ball, Launch1.transform.position, transform.rotation) as Rigidbody;
newBall.AddForce (new Vector3 (-22, 0, -15));
Timer = 0;
}
else if (Timer == 30) {
newBall = Instantiate (Ball, Launch2.transform.position, transform.rotation) as Rigidbody;
newBall.AddForce (new Vector3 (-22, 0, -15));
}
else if (Timer == 20) {
newBall = Instantiate (Ball, Launch3.transform.position, transform.rotation) as Rigidbody;
newBall.AddForce (new Vector3 (-22, 0, -15));
}
else if (Timer == 10) {
newBall = Instantiate (Ball, Launch4.transform.position, transform.rotation) as Rigidbody;
newBall.AddForce (new Vector3 (-22, 0, -15));
Debug.Log ("YOLO");
}
}
}
It probably has to do with time being a float, and you checking if a float value is the same as an int value. It's probably better if you check if time is bigger than 10, and if so instantiate and then reset time. Also, consider pooling: instantiating everything you need in Awake(), but in a disabled state. Then simply enable them when you need them.
Answer by hexagonius · Sep 17, 2015 at 08:33 AM
I agree with Rostam24. check for the time being greater then a reference value.if so, instantiate and reset time. change the reference value if you must, either by Random value or values in a list or just continuously adding or subtracting... whatever the case.
If your not in mobile, pooling might be too much an effort and also probably an advanced technique for you.
Your answer
Follow this Question
Related Questions
I got Some Error about how to Transform Postiton Enemy where one point can spawn 2 enemies. 1 Answer
How does one continuously move an object forward without using velocity? 1 Answer
Adding different values to randomly created objects 0 Answers
Need a variable to exist in the current context - not sure where to start 0 Answers
While instantiating getting object reference not set for no reason? 1 Answer