Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Accessing variables from other scripts

Discussion in 'Scripting' started by Soumikbhat, Jul 24, 2014.

  1. Soumikbhat

    Soumikbhat

    Joined:
    Nov 23, 2013
    Posts:
    110
    Here's something that i was wondering :

    How do we access variables from another script in C#?

    Like suppose we have in script Shoot.cs a float variable k, how do we access this variable from another script Walk.cs?

    And what if these scripts are not attached to the same gameobject? How does it differ if they are? I searched through this link, but could not find it. Would love to see some discussion on this...
     
  2. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    Check out my newest tutorial on Getcomponet in my signature.
    Best,
    Jon
     
  3. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
    you make the attribute public, than give the class its in a static method that lets you grab the currently running instance of the object.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Exemple : MonoBehaviour
    5. {
    6.     public static Exemple main;
    7.  
    8.     public string myVar = "My Global String";
    9.  
    10.     public static Exemple GetInstance
    11.     {
    12.         get { return main; }
    13.     }
    14.  
    15.     void Awake()
    16.     {
    17.         if (main == null)
    18.             main = this;
    19.     }
    20.  
    21.     void Update()
    22.     {
    23.  
    24.     }
    25. }
    Than in the accessing script you can do something like this.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Exemple2 : MonoBehaviour {
    5.  
    6.     // Use this for initialization
    7.     void Start () {
    8.  
    9.     }
    10.  
    11.     // Update is called once per frame
    12.     void Update () {
    13.         Debug.Log(Exemple.main.myVar);
    14.  
    15.     }
    16. }
    17.  
    This approach is best suite to things like manager or logic objects that get called from lots of other objects.

    For simply just getting a var or running a method of a other script on a object, getComponent<T>() is your best bet.
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Second chunk of code in the first example... (there is a language toggle on the right if you didn't notice it... looks like it defaults to js/unityscript)

    foo is your "variable k"
     
    Soumikbhat likes this.
  5. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    What cmc is a singleton manager. I don't think that's what you're asking. Use GetComponent. Singleton managers are nice because you can lazy instantiate them, but like he said, use it for managers. If you just was normal class inheritance, use get component. (my video)
     
  6. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
    Ya the singleton manager i use for my main logic class, and my menu logic, since its easy to make calls to it from anywhere this way.

    Also a way around get component, is to just define public attributes on your class, that take in the kinda component you want.

    Like say i got UILabel on a game object, than got a other script with
    Code (CSharp):
    1. public UILabel MyLabel;
    in it, i can drag the object with the label component onto it in the inspector, and it will grab the component, instead of the gameObject or transform.