Search Unity

Instantiate only if munition left

Discussion in 'Scripting' started by StefanB95, Apr 15, 2011.

  1. StefanB95

    StefanB95

    Joined:
    Mar 14, 2011
    Posts:
    97
    Hello guys i wanna to make that i can instantiate some item just if i got munition if i can call it so. I plan it this way
    ps: this script is from my head now so some errors can be there found
    I don't now how to make it don't work if clips are equal to zero
    i tryed it from the machine gun script to copy some thinks but it didn't work so if somebody can help me a litlle it would be nice
     
  2. billykater

    billykater

    Joined:
    Mar 12, 2011
    Posts:
    329
    Code (csharp):
    1.  
    2. var item : Transform;
    3. var clips = 2;
    4.  
    5. function Start(){
    6.    //This isn't needed as start is called just before the first update so update will be called directly after this
    7.    /*if (clips>0)
    8.    Update();*/
    9. }
    10.  
    11. function Update() {
    12.    if (InpuGetDown("Fire") [B] clips > 0[/B]) {
    13.       Instantiate (enemy, transform.position, Quaternion indentity);
    14.       clips--
    15.     }
    16. }
    17.  
     
  3. StefanB95

    StefanB95

    Joined:
    Mar 14, 2011
    Posts:
    97
    So all i just needed to add whas a part that will check if clips is > 0 but what is doing so i can now where to add :) ?
     
  4. billykater

    billykater

    Joined:
    Mar 12, 2011
    Posts:
    329
    there are several combinations. the 3 most use ones are !, || and

    These are called logic operators which can be applied to any boolean

    Code (csharp):
    1.  
    2. //! means "not"
    3. !true == false
    4. !false == true
    5.  
    Code (csharp):
    1.  
    2. // || means "or"
    3. true || true == true
    4. true || false == true
    5. false || true == true
    6. false || false == false
    7.  
    Code (csharp):
    1.  
    2. //  means "and"
    3. true  true == true
    4. true  false == false
    5. false  true == false
    6. false  false == false
    7.  
    Code (csharp):
    1.  
    2. InpuGetDown("Fire")  //is a function that returns true based if the value is true
    3.  
    4. clips > 0 //also evaluates to a boolean that is true if clips > 0 :-)
    5.  
    6. so
    7. if(InpuGetDown("Fire")  clips > 0) //Only if InputGetDown is true [B]and [/B] clips > 0 is true do the things contained in { }
    8.