- Home /
Question by
NomDeiX · May 30, 2017 at 07:25 PM ·
unity 52d gamerespawnspawnpoints
Why doesnt my respawn script work?
Hello community, I created this script, which should work like this : If the ball goes to collider, it will show goal canvas, wait 7 seconds and spawn players and ball back to starting positions. The problem comes after the goal, players and ball is spawned right nut after that they cannot move. Game is in 2d enviroment.
void Update()
{
if (startgoal == true)
Timer -= Time.deltaTime;
if (Timer <= 0.0f)
{
Goal.gameObject.SetActive(false);
GoalSound.Stop();
ball.transform.position = destination.position;
player1.gameObject.SetActive(true);
player2.gameObject.SetActive(true);
ball.SetActive(true);
startgoal = false;
player1.transform.position = destinationp1.position;
player2.transform.position = destinationp2.position;
}
}
void OnCollisionEnter2D(Collision2D collision)
{
Goal.gameObject.SetActive(true);
GoalSound.Play();
ball.SetActive(false);
ball.transform.position = destination.position;
Debug.Log("Goal!");
startgoal = true;
player1.gameObject.SetActive(false);
player2.gameObject.SetActive(false);
}
}
Thank you soo much
Comment
Answer by ShadyProductions · May 30, 2017 at 07:24 PM
if (Timer <= 0.0f)
{
Goal.gameObject.SetActive(false);
GoalSound.Stop();
ball.transform.position = destination.position;
player1.gameObject.SetActive(true);
player2.gameObject.SetActive(true);
ball.SetActive(true);
startgoal = false;
player1.transform.position = destinationp1.position;
player2.transform.position = destinationp2.position;
}
This logic is constantly being called each frame because you never update your timer after it first triggers, so it keeps setting
player1.transform.position = destinationp1.position;
player2.transform.position = destinationp2.position;
Answer by game4444 · Jun 03, 2017 at 11:33 AM
Edit your code like this if (Timer
startgoal = false; Timer = 5f; }