Search Unity

Use and change a variable of another script;

Discussion in 'Scripting' started by ppddgg, Jul 1, 2012.

  1. ppddgg

    ppddgg

    Joined:
    Jun 24, 2012
    Posts:
    6
    I have this script but doesn't work as I would like:

    Code (csharp):
    1. ExampleScript myExampleScript;
    2.  
    3.  
    4.  
    5.     public  float speed = 1;
    6.  
    7.     public float speedReturn = 1;
    8.  
    9.     public float MoreSpeed = 2;
    10.  
    11.        
    12.  
    13.     void Awake(){
    14.  
    15.         myExampleScript = gameObject.GetComponent<ExampleScript>();
    16.  
    17.     }
    18.  
    19.    
    20.  
    21.     void Update () {
    22.  
    23.        
    24.  
    25.         //Movement
    26.  
    27.         transform.Translate(0,-speed,0);
    28.  
    29.            
    30.  
    31.         //MoreSpeed
    32.  
    33.         if(myExampleScript.myMoreSpeed > 0  (Input.touchCount > 0  Input.GetTouch(0).phase == TouchPhase.Began)){
    34.  
    35.                 speed = MoreSpeed;  
    36.  
    37.             }
    38.  
    39. //LessSpeed
    40.  
    41.         if(speed == MoreSpeed){
    42.  
    43.                 Invoke ("SpeedInvokeFalse", 1);    
    44.  
    45.             }
    46.  
    47.     }
    48.  
    49.    
    50.  
    51.     //LessSpeed and myMoreSpeed -1
    52.  
    53.     void SpeedInvokeFalse(){
    54.  
    55.         speed = speedReturn;
    56.  
    57.                 myExampleScript.myMoreSpeed =  myExampleScript.myMoreSpeed - 1;
    58.  
    59.     }
    60.  
    61.  
    62.  
    63. }
    I would like to access to the variable "myMoreSpeed" which the value is 1, in another script and if this variable is greater than 0, and the player touch the touchscreen, the speed increases for 1 sec. After do that I would like to return the "myMoreSpeed", in the other script, to 0.

    If you find typos I apologize, I'm Italian and i don't speak english very well. Thanks!
     
  2. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    to access another variable there are two methods that I know of.


    GetComponent for non static vars.

    Using JS.
    Code (csharp):
    1.  
    2. var thisObject : GameObject;
    3. var thisData : theNameOfTheScript;
    4.  
    5. function Start()
    6. {
    7. thisData = thisObject.GetComponent(theNameOfThisScript);
    8. }
    9.  



    Of course the other way is with a static var.

    Again, in JS.
    Code (csharp):
    1.  
    2. ///with in XScript, create the var
    3. static var thisStat : Type;
    4.  
    Code (csharp):
    1.  
    2. ///within the YScript, call static var the script name it was created in
    3. XScript.thisStat = WhatYouWant;
    4.  
     
  3. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Just be careful with static variables, as they will work at all times in all cases and don't even need to be instantiated on a game object in your game. They are powerful, static variables, so use them wisely.

    There could be a couple of issues ppddgg, especially as we don't have a complete script to work with. What part is not working for you?

    ... and a tiny note re: var thisData : theNameOfTheScript; ... In theory, "theNameOfTheScript" is a type, and should start with a capital letter: var thisData : TheNameOfTheScript; Other examples are as in var myVarName : MyType; and var myScriptName : MyScriptName
     
  4. ppddgg

    ppddgg

    Joined:
    Jun 24, 2012
    Posts:
    6
    I want to put some cube in the scene. When the player touch one of them improve the variable "myMoreSpeed" by 1.

    This is the Example script which do that.


    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ExampleScript : MonoBehaviour {
    5.    
    6.     public int myMoreSpeed = 0;
    7.    
    8.     void OnTriggerEnter(Collider other){
    9.  
    10.         if(other.gameObject.name == "MoreSpeed"){
    11.         myMoreSpeed++;
    12.         }
    13.                
    14.     }
    15.    
    16.     void OnGUI(){
    17.  
    18.         GUILayout.Label ("MoreSpeed: " + myMoreSpeed);
    19.        
    20.     }
    21.    
    22.    
    23.                
    24. }
    Works fine

    Now in another script I want to call the variable myMoreSpeed and if the player touch the touchscreen increase the speed of the object (And this works), but only if the value of the variable myMoreSpeed is greater than 0 and every time the player touch the screen and increases the speed, myMoreSpeeddecreses by 1 point.

    This is the other script:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MovementExample : MonoBehaviour {
    5.    
    6.     ExampleScript myExampleScript; //<--- Works?
    7.     public float myMoreSpeedA;//<--Works?
    8.  
    9.     public  float speed = 1;
    10.     public float speedReturn = 1;
    11.     public float MoreSpeed = 2;  //<--This is another variable. Works Fine!
    12.    
    13.    
    14.     void Awake(){
    15.         myCatchMonetaScript = GetComponent("ExampleScript") as ExampleScript;//<--???
    16.                 myMoreSpeedA = myExampleScript.myMoreSpeed;//<--??
    17.     }
    18.    
    19.     void Update () {
    20.            
    21.         //Caduta pallina
    22.         transform.Translate(0,-speed,0);
    23.                
    24.         //MoreSpeed
    25.         if(myMoreSpeedA.myMoreSpeed > 0  /* <-- ?????*/ (Input.touchCount > 0  Input.GetTouch(0).phase == TouchPhase.Began)){
    26.                 speed = MoreSpeed; 
    27.             }
    28.         if(speed == MoreSpeed){
    29.                 Invoke ("SpeedInvokeFalse", 1); //<-- this is to normalize the speed after 1 sec   
    30.             }
    31.     }
    32.    
    33.     //MoreSpeed
    34.     void SpeedInvokeFalse(){
    35.         speed = speedReturn;
    36. myMoreSpeedA.myMoreSpeed = myMoreSpeedA.myMoreSpeed - 1; //<---Can this decrese by 1 "myMoreSpeed"
    37.     }
    38.  
    39. }
    I don't understand where is the problem
     
    Last edited: Jul 2, 2012
  5. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Accessing other GameObject and Components:

    [URL="http://docs.unity3d.com/Documentation/ScriptReference/]ScriptReference[/URL]
    [URL="http://docs.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html]Accessing_Other_Game_Objects[/URL]
    [URL="http://docs.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Components.html]Accessing_Other_Components[/URL]

    In a nutshell - Accessing other components thru a public reference:

    Create a new test scene.
    Create a C# script and call it "ScriptA".
    Replace the content with this code:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ScriptA : MonoBehaviour {
    5.     public bool boolA;
    6. }
    Create a C# script and call it "ScriptB".
    Replace the content with this code:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ScriptB : MonoBehaviour {
    5.    
    6.     public ScriptA sameScriptA;
    7.     public ScriptA differentScriptA;
    8.  
    9.     void Start () {
    10.         Debug.Log ("Same Script: " + sameScriptA.boolA);
    11.         Debug.Log ("Different Script: " + differentScriptA.boolA);
    12.     }
    13. }
    Create two new GameObjects.
    Call one "Different Object".
    Call the other "SameObject".

    Your test scene should look like this:


    Attach to "DifferentObject" one copy of ScriptA:


    Attach to "SameObject" one copy of ScriptA and one copy of ScriptB.
    Set the "Bool A" in "ScriptA" to "true".
    Populate the references in ScriptB with a reference to "DifferentObject" and "SameObject":


    Enter play mode and look in the console.
    You should see the reference to the two scripts and the values on the variables:


    This is the basis of grabbing a reference to a component thru a public variable.