- Home /
Bug when reloading scene after pushing respawn button
My project is recreating "Space Invaders". I am using a separate scene for the death menu (DeathScene). When the alienBullet hit the SpaceShip, this scene is loaded. In the DeathScene I made a Respawn button that loads again the main scene ( Scenemanager.Loadscene(1); ). But when I reload the main scene, every single alien drops an alienBullet instantly (even if they aren't supposed to, in the first load of the scene they start dropping the alienBullets randomly after 3 seconds), and after that they come back to working normally. hope you can help me... here is my allien code where I control the alienBullet firing inside the Fixed Update:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Alien : MonoBehaviour
{
public float speed = 10;
private Rigidbody2D rigidBody;
public Sprite startingImage;
public Sprite altImage;
private SpriteRenderer spriteRenderer;
public float secBeforeSpriteChange = 0.5f;
public GameObject alienBullet;
public float minFireRateTime = 1.0f;
public float maxFireRateTime = 3.0f;
public float baseFireWaitTime = 3.0f;
public Sprite explodedShipImage;
void Start()
{
rigidBody = GetComponent<Rigidbody2D>();
rigidBody.velocity = new Vector2(1, 0) * speed;
spriteRenderer = GetComponent<SpriteRenderer>();
StartCoroutine(ChangeAlienSprite());
baseFireWaitTime = baseFireWaitTime + Random.Range(minFireRateTime, maxFireRateTime);
}
void Turn(int direction)
{
Vector2 newVelocity = rigidBody.velocity;
newVelocity.x = speed * direction;
rigidBody.velocity = newVelocity;
}
void MoveDown()
{
Vector2 position = transform.position;
position.y -= 1;
transform.position = position;
}
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.name == "leftWall")
{
Turn(1);
MoveDown();
}
if (col.gameObject.name == "rightWall")
{
Turn(-1);
MoveDown();
}
if (col.gameObject.tag == "Bullet")
{
soundManager.Instance.playOneShot(soundManager.Instance.AlienDies);
Destroy(gameObject);
}
}
public IEnumerator ChangeAlienSprite()
{
while (true)
{
if (spriteRenderer.sprite == startingImage)
{
spriteRenderer.sprite = altImage;
soundManager.Instance.playOneShot(soundManager.Instance.AlienBuzz1);
}
else
{
spriteRenderer.sprite = startingImage;
soundManager.Instance.playOneShot(soundManager.Instance.AlienBuzz2);
}
yield return new WaitForSeconds(secBeforeSpriteChange);
}
}
void FixedUpdate()
{
if (Time.time > baseFireWaitTime)
{
baseFireWaitTime = baseFireWaitTime +
Random.Range(minFireRateTime, maxFireRateTime);
Instantiate(alienBullet, transform.position, Quaternion.identity);
}
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == "Player")
{
soundManager.Instance.playOneShot(soundManager.Instance.ShipExplosion);
col.GetComponent<SpriteRenderer>().sprite = explodedShipImage;
Destroy(gameObject);
DestroyObject(col.gameObject, 0.5f);
}
}
}
Answer by unity_ek98vnTRplGj8Q · Aug 17, 2020 at 09:37 PM
You are using Time.time in your code
void FixedUpdate()
{
if (Time.time > baseFireWaitTime)
{
baseFireWaitTime = baseFireWaitTime +
Random.Range(minFireRateTime, maxFireRateTime);
Instantiate(alienBullet, transform.position, Quaternion.identity);
}
}
This tracks the time since the start of your application, but you actually want the time since the start of your scene. I recommend using your own variable to keep track of time
float sceneTime;
void Start(){
sceneTime = 0.0f;
}
void FixedUpdate () {
sceneTime += Time.fixedDeltaTime;
if (sceneTime > baseFireWaitTime) {
baseFireWaitTime = Random.Range (minFireRateTime, maxFireRateTime);
Instantiate (alienBullet, transform.position, Quaternion.identity);
}
}
O$$anonymous$$G! This actually worked! Just got to make one correction of your code, figured it out after testing:
baseFireWaitTime = Random.Range ($$anonymous$$FireRateTime, maxFireRateTime);
should acutally be:
baseFireWaitTime += Random.Range ($$anonymous$$FireRateTime, maxFireRateTime);
Other than that (which created another interesting bug, you saved my day, thank you!