Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Script for gun - error.

Discussion in 'Scripting' started by JAMJAN, Mar 19, 2013.

  1. JAMJAN

    JAMJAN

    Joined:
    Jan 27, 2013
    Posts:
    27
    Hey. I'm trying to write script for gun. But it's not working, becouse there is 3 errors. Code:

    Code (csharp):
    1.     var Zero = 0;
    2.     var Clip = 8;    
    3.     var Bullet : GameObject;
    4.     var Spawn : GameObject;
    5.     var BulletSpeed : float = 100.0;
    6.      
    7.     function Update(){
    8.      
    9.         if(Input.GetButtonDown("Fire1")){
    10.         if (Clip > Zero){
    11.             Fire();
    12.         yield WaitForSeconds(0.5);
    13.         }
    14.         }
    15.        
    16.         if (Clip = Zero){
    17.      Reolad();
    18.     }
    19.     }
    20.      
    21.      
    22.     function Fire(){
    23.      
    24.         var bullet = Instantiate(Bullet, Spawn.transform.position, transform.rotation);
    25.         bullet.rigidbody.AddForce(Vector3.forward * BulletSpeed);
    26.         clip - 1
    27.     }
    28.    
    29.     function Reolad(){
    30.    
    31.     yield WaitForSeconds(2);
    32.    
    33.    Clip + 8
    34.    }
    Errors:
    Code (csharp):
    1.  
    2. Assets/skrypty/new springfield.js(17,14): BCE0044: expecting :, found ';'.
    3.  
    4. Assets/skrypty/new springfield.js(16,20): BCE0043: Unexpected token: Zero.
    5.  
    6. Assets/skrypty/new springfield.js(16,18): BCE0044: expecting ), found '='.
     
  2. Glockenbeat

    Glockenbeat

    Joined:
    Apr 24, 2012
    Posts:
    669
    Code (csharp):
    1.  
    2. if (Clip = Zero){
    3.  
    needs to be

    Code (csharp):
    1.  
    2. if (Clip == Zero){
    3.  
    Equality comparison always is == (or ===). Plus you should make sure to always give a type to your variables, like

    Code (csharp):
    1.  
    2. var Clip : int = 8;
    3.  

    It's also common practice to give variables a camelCased name starting with a lowercase letter, and a method a camelCased name starting with uppercase letter. Just to make sure that you are able to distinguish both at any given time:

    Code (csharp):
    1.  
    2. var bulletSpeed: float = 100.0;
    3.  
    4. function ReloadWeapon() {
    5. }
    6.  
     
    Last edited: Mar 19, 2013
  3. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    For comparisons, use ==, = is assignement

    if (Clip == Zero){

    Reolad();

    }
     
  4. JAMJAN

    JAMJAN

    Joined:
    Jan 27, 2013
    Posts:
    27
    Now there is no errors. Code:

    Code (csharp):
    1.     var Zero : int = 0;
    2.     var Clip : int = 8;    
    3.     var Bullet : GameObject;
    4.     var Spawn : GameObject;
    5.     var BulletSpeed : float = 100.0;
    6.      
    7.     function Update(){
    8.      
    9.         if(Input.GetButtonDown("Fire1")){
    10.         if (Clip > Zero){
    11.             Fire();
    12.         yield WaitForSeconds(0.5);
    13.         Clip --;
    14.         }
    15.         }
    16.        
    17.         if (Clip == Zero){
    18.      ReoladWeapon();
    19.     }
    20.     }
    21.      
    22.      
    23.     function Fire(){
    24.      
    25.         var bullet = Instantiate(Bullet, Spawn.transform.position, transform.rotation);
    26.         bullet.rigidbody.AddForce(Vector3.forward * BulletSpeed);
    27.        }
    28.    
    29.     function ReoladWeapon(){
    30.    
    31.     yield WaitForSeconds(2);
    32.    
    33.    Clip += 8;
    34.    }
    But it no worka that as i want. It should look that:
    -shot,
    -bulet -1
    -wait (yield)
    -next shot
    -if clip =00, stop
    -reolad

    What is wrong?
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you can't use waitforseconds within the Update function... you can only yield within the coroutine functions themselves.

    check out:

    http://forum.unity3d.com/threads/73101-Function-Update-and-yield-WaitForSeconds

    ps try to layout your code a little more regularly, it really helps with the readability and makes it simpler to spot mistakes and errors. For example, the update function from above should be laid out a little like this (haven't fixed it, just changed the layout and formatting):

    Code (csharp):
    1.  
    2. function Update()
    3. {
    4.     if(Input.GetButtonDown("Fire1"))
    5.     {
    6.         if (Clip > Zero)
    7.         {
    8.             Fire();
    9.             yield WaitForSeconds(0.5);
    10.             Clip --;
    11.         }
    12.     }
    13.  
    14. if (Clip == Zero)
    15.     {
    16.      ReoladWeapon();
    17.     }
    18. }
    19.  
     
    Last edited: Mar 20, 2013
  6. JAMJAN

    JAMJAN

    Joined:
    Jan 27, 2013
    Posts:
    27
    Now is look that:

    Code (csharp):
    1.     var Zero : int = 0;
    2.     var Clip : int = 8;    
    3.     var Bullet : GameObject;
    4.     var Spawn : GameObject;
    5.     var BulletSpeed : float = 100.0;
    6.      
    7.     function Update(){
    8.      
    9.         if(Input.GetButtonDown("Fire1")){
    10.         if (Clip > Zero){
    11.             Fire();
    12.         MyUpdate1();
    13.         Clip --;
    14.                         }
    15.                                         }
    16.        
    17.         if (Clip == Zero){
    18.      ReoladWeapon();
    19.                          }
    20.                      }
    21.      
    22.      
    23.     function Fire(){
    24.      
    25.         var bullet = Instantiate(Bullet, Spawn.transform.position, transform.rotation);
    26.         bullet.rigidbody.AddForce(Vector3.forward * BulletSpeed);
    27.                    }
    28.    
    29.     function ReoladWeapon(){
    30.     MyUpdate2();
    31.     Clip += 8;
    32.                            }
    33.    
    34.    function MyUpdate1(){
    35.    yield WaitForSeconds(0.5);
    36.                        }
    37.                        
    38.    function MyUpdate2(){
    39.    yield WaitForSeconds(2);
    40.                        }
    Work as before.
     
  7. EliteMossy

    EliteMossy

    Joined:
    Dec 2, 2012
    Posts:
    513
    Code (csharp):
    1. var Clip : int = 8;
    2.     var Bullet : GameObject;
    3.     var Spawn : GameObject;
    4.     var BulletSpeed : float = 100.0;
    5.     var nextFireTime : float;
    6.     var RateOfFire : float = 0.5f;
    7.  
    8.     function Start() {
    9.         nextFireTime = Time.time + RateOfFire;
    10.     }
    11.    
    12.     function Update() {
    13.         if (Input.GetButtonDown("Fire1")  Time.time > nextFireTime  Clip > 0) {
    14.                 Fire();
    15.         nextFireTime = Time.time + RateOfFire;
    16.                 Clip--;
    17.         }
    18.         if (Clip <= 0) {
    19.             StartCoroutine(ReoladWeapon());
    20.         }
    21.     }
    22.     function Fire() {
    23.         var bullet = Instantiate(Bullet, Spawn.transform.position, transform.rotation);
    24.         bullet.rigidbody.AddForce(Vector3.forward * BulletSpeed);
    25.     }
    26.    
    27.     function ReoladWeapon() {
    28.         yield WaitForSeconds(2f);
    29.         Clip += 8;
    30.     }
    This is how it should be done
     
  8. JAMJAN

    JAMJAN

    Joined:
    Jan 27, 2013
    Posts:
    27
    Still the same
     
  9. EliteMossy

    EliteMossy

    Joined:
    Dec 2, 2012
    Posts:
    513
    What is still the same?
     
  10. JAMJAN

    JAMJAN

    Joined:
    Jan 27, 2013
    Posts:
    27
    If I click Fire 1 soot all the time.
     
  11. JAMJAN

    JAMJAN

    Joined:
    Jan 27, 2013
    Posts:
    27
    Hi. Now J have workin script for weapon. Script throw bullet from spawn poin. Game will be Third Person Shooter. Crosshair will be in the midle of the screen. Player, weapon and spawn point on the left corner. I'm thinking about aiming system. Maybe Main Camera creat target point on first surface and spawn point will be turn on it?