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

Ammo

Discussion in 'Scripting' started by 787gabriel777, Dec 15, 2015.

  1. 787gabriel777

    787gabriel777

    Joined:
    Jul 20, 2015
    Posts:
    65
    Hello.
    In my script, when i click on the mouse button, the ammo of my caracter goes to -1 or -2, and, he only instantiate ONE bullet of SIX.
    what is wrong?
    Code (JavaScript):
    1.     if (tipo == 1){
    2.         if(guicentral.ammohand >= 1){
    3.             if(Input.GetMouseButton(0)){
    4.                 Instantiate(bullet,canoarma.position,canoarma.rotation);
    5.                 guicentral.ammohand =- 1;
    6.             }
    7.      
    8.         }
    9.     }
     
    Last edited: Dec 15, 2015
  2. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    where do you have 6 bullets ? you just instantiate one bullet.

    here you are setting ammohand to -1 did you mean

    guicentral.ammohand = guicentral.ammohand -1 ?
    that would be

    Code (CSharp):
    1. guicentral.ammohand -= 1;
     
    Nigey likes this.
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
    You're doing Input.GetMouseButton instead of Input.GetMouseButtonDown.

    GetMouseButton returns true on every frame as long as the mouse button is held down. This means that you're going to spawn everything in just six frames. You probably think there's only six bullets because they're inside of each other.

    Just use GetMouseButtonDown instead.
     
  4. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    In fact that is also a problem, but it doesn't get so far,

    guicentral.ammohand can be 100, but when it enters the if(guicentral.ammohand>=1) once guicentral.ammohand is set to -1 and it wont enter if(guicentral.ammohand>=1) any more, therefore it's just one spawn
     
  5. 787gabriel777

    787gabriel777

    Joined:
    Jul 20, 2015
    Posts:
    65
    ok thanks for all