Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Save bullets in magazine

Discussion in 'Scripting' started by Racm_Games, Dec 17, 2015.

  1. Racm_Games

    Racm_Games

    Joined:
    Sep 23, 2015
    Posts:
    19
    Hi everyone,
    I am currently developing a shooter and I am trying to save the ammo so that when the player switches between guns, the ammo count doesnt reset.

    Well, the problem relies that when equipping a gun the gameobject is deleted, and the other gun instantiates in the weapon`s spot. This causes that when spawning the new gun the gun`s values reset as if it was a new gun.

    This is the method I use to equip new guns
    Code (CSharp):
    1.     public void EquipGun(Gun gunToEquip)
    2.     {
    3.         Gun gunOld = equippedGun;
    4.         equippedGun = Instantiate (gunToEquip, weaponHold.position,weaponHold.rotation) as Gun;
    5.         equippedGun.transform.parent = weaponHold;
    6.  
    7.         if (gunOld != null ) {
    8.             equippedGun.RefreshMagazine();
    9.             Destroy(gunOld.gameObject);
    10.         }
    11.     }
    Currently I have the magazine class inside the gun class.. but it doesnt seem to work... neither having the magazine class apart in a different class.

    The RefreshMagazine method calls 2 other methods that save the current bullet in the magazine and the total bullets inside the gun. But I dont understand why it doesnt work.
     
  2. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Could you have all guns on the player & just deactivate the current gun & then activate the new gun rather than destroy them?
     
  3. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    Code (CSharp):
    1.  if (gunOld != null ) {
    2.             gunOld .RefreshMagazine();
    3.            Destroy(gunOld.gameObject);
    4. }
    but this also wouldn't make sense, saving something in an instatiated (a copy) gameobject and than destroying it. your weapon prefab will never get this values.

    also you should handle ammo and weapons separately so if you have weapons that can use the same ammo typ.
    Your weapon just has to have a reference/enum/string so you can get the amount of stored ammo of typ XYZ.

    And i would not destroy the weapon every time is not so good. when you think about someone pushing change weapon key permanently just 4 fun and every time your weapon is destroy instantiated ... garbage .... :D

    Try to make a Weapon Manager holding all weapons your character has equiped.
     
  4. Recon03

    Recon03

    Joined:
    Aug 5, 2013
    Posts:
    839

    agreed, deactivate , and reactivate works as well when equipping weapons and such, but destroying them is terrible for performance. Also pool your bullets.. Make a pool Manager.. Very easy to do and a ton of tuts now a days. If you still don't understand, plenty of assets that are cheap for pooling. Good luck!
     
  5. Racm_Games

    Racm_Games

    Joined:
    Sep 23, 2015
    Posts:
    19
    Hey thanks!, I didnt know about object pooling... Watching a tut right now.
     
  6. Racm_Games

    Racm_Games

    Joined:
    Sep 23, 2015
    Posts:
    19
    Yeah I did notice this afternoon that it wasnt making sense... But thanks for your advise! I am gonna go with the object pooling Recon03 suggested.. oh and about the weapon handler I kind of have that but because I didnt know about object pooling I didnt implement it
     
  7. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Destroying & instantiating guns over & over isn't very efficient but if your game is small it should be ok. Good luck
     
  8. Racm_Games

    Racm_Games

    Joined:
    Sep 23, 2015
    Posts:
    19
    Thanks for the advise.. now that you mention it, In my game there are also waves of enemies, should I use the object pooling technique also??

    Right now is a small game but it could get bigger... and also is a mobile game so it needs to be efficient
     
  9. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    If you are spawning & destroying lots of the same thing pooling is more efficient. You just need to work out what the maximum onscreen is likely to be & set the pool just above that or create the pool so it is expandable.