Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TheNucleaLion · Aug 17, 2020 at 09:06 PM · scene-loadingscene-switchingfixedupdaterespawningspace invaders

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);
         }
     }
 
 
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
        }
 
      }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TheNucleaLion · Aug 17, 2020 at 09:52 PM 0
Share

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!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

137 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Scenes: Are previous scenes still active after you call a new scene? 1 Answer

Have scene loaded in background 1 Answer

LoadSceneAsync makes unity behave differently after build. 0 Answers

AllowSceneActivation only works on first inactive scene? 0 Answers

Fadeout Issue 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges