Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  5. Nominations have been announced for this years Unity Awards. Celebrate the wonderful projects made by your peers this year and get voting! Vote here!
    Dismiss Notice
  6. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Explosion code bug

Discussion in 'Scripting' started by GVT017, Dec 6, 2018.

  1. GVT017

    GVT017

    Joined:
    May 16, 2018
    Posts:
    57
    Hello! I was in the process of making my game, and around 2 weeks ago, I spotted a bug I can't fix.

    In the game, you have explosive bullets, and when they contact with anything, they explode, and then they cant explode more than once. However, a bullet should only explode once, but sometimes they do something...weird. When the previously already exploded bullet has no contact with anything, and a new bullet gets fired, when the new one contacts with anything, the previous one explodes again, and the one you just fired does not!

    Code for firing the bullet:
    Code (CSharp):
    1. if (Input.GetMouseButton(0) && ammo > 0 && Time.time >= nextTimeToFire && bulletType == 2)
    2.         {
    3.             BulletPickUp.hasExploded = false;
    4.             nextTimeToFire = Time.time + 1f / fireRate;
    5.             grenadeInstance = Instantiate(grenade, gunEnd.position, gunEnd.rotation) as Rigidbody;
    6.             grenadeInstance.velocity = gunEnd.up * gunForce;
    7.             ammo--;
    8.             if (gunNum == 4)
    9.             {
    10.                 saglSFX.Play();
    11.             }
    12.             StartCoroutine("ShootAnimation");
    13.         }
    Code for exploding the bullet:
    Code (CSharp):
    1. if (!hasExploded)
    2.             {
    3.                 Explode();
    4.                 GameObject explosionInstance;
    5.                 explosionInstance = Instantiate(explosionSource, gameObject.transform.position, gameObject.transform.rotation);
    6.                 hasExploded = true;
    7.             }
    8.  
    9.             if (col.gameObject.CompareTag("Player"))
    10.             {
    11.                 GunFiring.ammo += 1;
    12.                 GameObject pickUpInstance;
    13.                 pickUpInstance = Instantiate(pickUpSource, gameObject.transform.position, gameObject.transform.rotation) as GameObject;
    14.                 Destroy(gameObject);
    15.             }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    2,886
    Particle systems generally are set to loop as long as they exist. Is that what you're seeing?

    To get around this I generally add a "TimeToLive" script that kills the entire explosion object after a certain amount. If you're using an explosion object pooler, just return the object to the pool after you shut it off after enough time has passed to see the poof.
     
  3. GVT017

    GVT017

    Joined:
    May 16, 2018
    Posts:
    57
    @Kurt-Dekker Well, that could be a solution, but the game works a bit differently than others. You should be able to pick up the very same bullet after you fired it, and the sound and effect are not on the bullet itself, but spawned in on a GameObject that later gets removed.
     
  4. GVT017

    GVT017

    Joined:
    May 16, 2018
    Posts:
    57
    Damn this site, I have to post this question for the third time again...
     
  5. LurkingNinjaDev

    LurkingNinjaDev

    Joined:
    Jan 20, 2015
    Posts:
    1,927
    Please don't create duplicates.

    And if you want some help, please edit your first post and give us more code because we don't know what are you using. I see the code you're running, but I a have hard time imagining in what context you're using this code.
     
  6. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    12,589
    We're a community that volunteers our assistance. Breaking the rules and being negative towards the community will only serve to decrease the likelyhood that people will assist you. You need to learn patience. We're not all sitting at our desks waiting for the next thread to show up. We have our own lives, our own jobs, our own families, etc.