Search Unity

Best way to handle this code piece. Opinion (Gun scripts)

Discussion in 'Scripting' started by ChuckieGreen, Feb 23, 2018.

  1. ChuckieGreen

    ChuckieGreen

    Joined:
    Sep 18, 2017
    Posts:
    358
    Hey, so most of the time I set my code up and later realise I should have done it a different way, so thought asking for an opinion would be better this time.

    So what I want is my player to have 3 guns, when a num key is pressed the gun will change (this part is already done). But I am now trying to figure out how to deal with the ammo etc. Would I be best with one script that holds variables for the 3 ammo types and have the firing etc code in there too, then attaching it to, say the player. Or would I be best having one script per gun that would obviously hold the ammo variable for the specific gun, and then attach that to the gun itself.

    I dont know if there is very much difference between these ways, but thought an opinion from people who know what they are talking about would be more helpful than my newbish ideas
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    For three (guns), it sounds like not a huge difference. I would still prefer to have the ammo for the gun on its own script, though.. and that would be easier to add new guns, too, if that ever happened.
     
  3. ChuckieGreen

    ChuckieGreen

    Joined:
    Sep 18, 2017
    Posts:
    358
    I will make single scripts then, thanks for the input.
     
  4. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    I tend make an IGun interface or a master Gun class, just take some time to think of all the common stuff between them
    Fire();
    Reload();
    etc();..