Search Unity

Change Variable from other script

Discussion in 'Scripting' started by The3DKnight, Mar 24, 2013.

  1. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    So i was thinking , and i can't find the solution (dumb)...
    I wanted like when i trigger a box and press E the value from script 2 to add to value in script 1
    So let's see :
    Script 1(Pick up script)
    Code (csharp):
    1.  
    2.  
    3. var script :  WeaponIndex;
    4. var Index : int = 2;
    5.  
    6. function OnTriggerEnter (other : Collider) {
    7.     if(other.gameObject.tag == "Weapons")
    8.     {
    9.        
    10.         Triggered = true;
    11.         Debug.Log("Weapon");
    12.         ShowGUI = true;
    13.        
    14.     }
    15. }
    16. function Update() {
    17. if (Triggered  Input.GetKey(KeyCode.E))
    18. {
    19. //add WpIndex from Script 2 to this script 1
    20. script = GetComponent("WeaponIndex");      
    21. Index == script.WpIndex;
    22.  
    23. }
    24.  
    25. }
    26.  
    And Script 2 is simple :

    Code (csharp):
    1.  
    2. var WpIndex : int = 2;
    3.  
    So real question is how to add value from Script 2 (WpIndex) to value in Script 1 (Index)?
    Any help,tips?

    Thank You...
     
  2. EliteMossy

    EliteMossy

    Joined:
    Dec 2, 2012
    Posts:
    513
    make WpIndex public, like public var WpIndex : int = 2;
     
  3. BPPHarv

    BPPHarv

    Joined:
    Jun 9, 2012
    Posts:
    318
    In this case the public isn't required. uJavaScript global variables are public unless marked otherwise.

    Code (csharp):
    1. Index == script.WpIndex;
    should read as
    Code (csharp):
    1. Index = script.WpIndex;
    Your version is testing for equality between Index and WpIndex you want an assignment which is a single equal sign.
     
    Last edited: Mar 25, 2013
  4. EliteMossy

    EliteMossy

    Joined:
    Dec 2, 2012
    Posts:
    513
    I only use C# so i get confused with unityscript.
     
  5. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    Hi it was
    i wrote wrong in here. But the script it was trying to access was named WeaponIndex 2 so i changed it to WeaponIndex .
    But now when i press E i get error

    Code (csharp):
    1.  
    2.  
    3. function Update ()
    4. {
    5. if (Triggered  Input.GetKey(KeyCode.E))
    6. {
    7. script = GetComponent("WeaponIndex");  
    8. //This is line 17 **************************   
    9. Index = script.WpIndex;
    10. }
    11.  
    12. }
    13.  
    I don't know what to do to fix this

    Can anybody help?
     
  6. Annihlator

    Annihlator

    Joined:
    Oct 15, 2012
    Posts:
    378
    Unity mentions it cannot find an object, this (in the case of c# atleast, i dont use unityscript myself) this can likely be the result of either;
    -The object the script is running from, does not contain a Component/script named WeaponIndex.
    -You tried looking up an object using GetComponent instead of GameObject.Find("ObjectName")

    Hope it'll get you somewhere :)
     
  7. BPPHarv

    BPPHarv

    Joined:
    Jun 9, 2012
    Posts:
    318
    I'd agree with Annihlator, from the code it's hard to tell but do you have the WeaponIndex.js script on the object that is the player or the object that is the gun?

    The way you've written it in terms of the GetComponent search will only work if both scripts are on the same object but from the collision section it looks like you are actually using this to detect which weapon the player bumped into so I dunno.. it's not clear from your example.

    I placed these two scripts on a single object and when I toggle the boolean 'Triggered' in the inspector then press the E key the value of Index changed from it's default -1 to 2.

    PickUp.js
    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. var script :  WeaponIndex;
    5. var Index : int = -1;
    6.  
    7. //unspecified variables required for operational reference but not
    8. //included in OP's example code
    9.  
    10. var Triggered : boolean = false;
    11. var ShowGUI : boolean = false;
    12.      
    13. function OnTriggerEnter (other : Collider)
    14.     {
    15.     if(other.gameObject.tag == "Weapons")
    16.         {
    17.         Triggered = true;
    18.         Debug.Log("Weapon");
    19.         ShowGUI = true;          
    20.         }
    21.     }
    22.    
    23. function Update()
    24.     {
    25.     if (Triggered  Input.GetKey(KeyCode.E))
    26.         {
    27.         //add WpIndex from Script 2 to this script 1
    28.         script = GetComponent("WeaponIndex");      
    29.         Index = script.WpIndex;    
    30.         }    
    31.     }
    32.  
    and

    WeaponIndex.js
    Code (csharp):
    1.  
    2. #pragma strict
    3. var WpIndex : int = 2;
    4.  
     
    Last edited: Mar 25, 2013
  8. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    I'm sorry i wasn't at home lately.
    But how can i change so i it can detect the WpIndex that's on other object?
    Since i want when i collide whit the weapon on ground to get that weapons number(Index)and with that number i can change weapon?
     
  9. EliteMossy

    EliteMossy

    Joined:
    Dec 2, 2012
    Posts:
    513