Search Unity

Using the same int on different scripts

Discussion in 'Scripting' started by TheSige, Jul 25, 2014.

  1. TheSige

    TheSige

    Joined:
    Mar 22, 2014
    Posts:
    9
    heya guys,

    So I know that this has been posted many times. But I couldn't really understand using the 'getcomponent' etc.
    I want to use a variable called 'aantal' to make you only pick up one weapon.
    The script I've made so far:
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var scimitaroppakken : boolean;
    4. public var aantal : int;
    5.  
    6. function OnTriggerEnter (other : Collider)
    7.  
    8. {
    9.     if(other.tag == "Player")
    10.    
    11.     {
    12.         scimitaroppakken = true;  
    13.     }
    14.  
    15. }
    16.  
    17. function OnTriggerExit (other : Collider)
    18.  
    19. {
    20.     if(other.tag == "Player")
    21.    
    22.     {
    23.         scimitaroppakken = false;  
    24.     }
    25.        
    26. }
    27.  
    28.  
    29. function Update ()
    30.  
    31. {
    32.     if(scimitaroppakken == true)
    33.    
    34.     {
    35.         if(Input.GetMouseButton(0))
    36.        
    37.         {
    38.        
    39.             if(aantal < 1)
    40.             {
    41.        
    42.                  gameObject.active = false;
    43.                  aantal = aantal + 1;
    44.             }
    45.         }
    46.     }
    47. }
    48.  
    49.  
    and script 2:

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var zwaardoppakken : boolean;
    4. public var aantal : int;
    5.  
    6. function OnTriggerEnter (other : Collider)
    7.  
    8. {
    9.     if(other.tag == "Player")
    10.    
    11.     {
    12.         zwaardoppakken = true;  
    13.     }
    14.  
    15. }
    16.  
    17. function OnTriggerExit (other : Collider)
    18.  
    19. {
    20.     if(other.tag == "Player")
    21.    
    22.     {
    23.         zwaardoppakken = false;  
    24.     }
    25.        
    26. }
    27.  
    28.  
    29. function Update ()
    30.  
    31. {
    32.     if(zwaardoppakken == true)
    33.    
    34.     {
    35.    
    36.         if(Input.GetMouseButton(0))
    37.        
    38.         {
    39.             if(aantal < 1)
    40.             {
    41.        
    42.                  gameObject.active = false;
    43.                  aantal = aantal + 1;
    44.             }
    45.         }
    46.     }
    47. }
    48.  
    49.  
    I dont understand why the public var aantal is not working on the other script
     
  2. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    Check out my signature for a get component tutorial that will help you out.

    Best,

    Jon
     
  3. TheSige

    TheSige

    Joined:
    Mar 22, 2014
    Posts:
    9
    Will do :)
    But I see you are working with C# and I am using javascript(unityscript)
     
  4. TheSige

    TheSige

    Joined:
    Mar 22, 2014
    Posts:
    9
    anybody else?
     
  5. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    It's the same for javascript and c#. It's the Unity API.

    That video tells you exactly what you need todo. I hope nobody just spoon feeds you the answer.

    Best of luck,

    Jon
     
  6. Pysassin

    Pysassin

    Joined:
    Dec 27, 2012
    Posts:
    87
    I cant understand WHAT you expect this script to do but if you want the variable to only have ONE instance across several copies you need to look into the static keyword. Ill not say more because it is a pretty basic programming concept and easily googled.