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

How to use Coroutine function and changing values of variables between different scripts

Discussion in 'Scripting' started by jameslol417, Jan 18, 2019.

  1. jameslol417

    jameslol417

    Joined:
    Jan 18, 2019
    Posts:
    7
    Hello, I just started learning Unity and I am facing some problems. I am trying to use the coroutine function, but I don't know how to use the function itself. Also, when I set the value of a valuable in one script, is it possible to change its value in another? ex) after defining k=1 in script A is it possible to state k=2 in another?
    I just started programming overall so it would be helpful if anyone can explain it very easily.
     
  2. MattM_Unity

    MattM_Unity

    Unity Technologies

    Joined:
    Aug 18, 2017
    Posts:
    45
    Hello jameslol417!

    Coroutines are very easy to create & use. From your post, I understand that you created a Coroutine but don't know how to call it? The following is how you do that:

    Code (CSharp):
    1. IEnumerator MyCoroutine()
    2. {
    3.     ...
    4. }
    5.  
    6. void MyFunction()
    7. {
    8.     StartCoroutine("MyCoroutine");
    9. }
    Now, to change a variable from another script, there are several options:

    If your variable is one of a kind (eg. player's health, score, in-game time...), you can set your variable to be public and static:

    Code (CSharp):
    1. public static int MyNumber;
    To call it from another script, simply type the name of the class in which the variable is in followed by the name of the variable:

    Code (CSharp):
    1. // Script A.
    2. public class MyClassA : MonoBehaviour
    3. {
    4.     public static int MyNumber;
    5.  
    6.     void Start()
    7.     {
    8.         MyNumber = 1;
    9.     }
    10. }
    11.  
    12. //Script B.
    13. public class MyClassB : MonoBehaviour
    14. {
    15.     void ChangeMyNumber()
    16.     {
    17.         MyClassA.MyNumber = 2;
    18.     }
    19. }
    However, if your variable isn't unique and is used across your game multiple times/in multiple instances, it is heavily recommended to not use static variables (as it will most likely end up in you facing a lot of problems). Here, your best option is to use the GetComponent() function to fetch the variable from another script. In order to do that, you need a reference to the script/GameObject containing the variable you want to access:

    Code (CSharp):
    1. // Script A.
    2. public class MyClassA : MonoBehaviour
    3. {
    4.     // Note: the variable needs to be set to public if you want to access it
    5.     // from another class.
    6.     public int MyNumber;
    7.  
    8.     void Start()
    9.     {
    10.         MyNumber = 1;
    11.     }
    12. }
    13.  
    14. //Script B.
    15. public class MyClassB : MonoBehaviour
    16. {
    17.     // Let's assume that Script A will be attached to this GameObject.
    18.     public GameObject MyGameObject;
    19.  
    20.     void Start()
    21.     {
    22.         // Not recommended if your scene contains a lot of GameObjects.
    23.         MyGameObject = GameObject.Find("MyGameObjectName");
    24.  
    25.         // or
    26.  
    27.         // If your GameObject is tagged.
    28.         MyGameObject = GameObject.FindGameObjectsWithTag("MyGameObjectTag");
    29.  
    30.         // or simply drag & drop the GameObject from your scene directly
    31.         // in the GameObject field of Script B's Inspector.
    32.     }
    33.  
    34.     void ChangeMyNumber()
    35.     {
    36.         // Here we are essentially fetching the component "Script A" from the GameObject and
    37.         // changing the value of its variable "MyNumber".
    38.         MyGameObject.GetComponent<MyClassA>().MyNumber = 2;
    39.     }
    40. }
    I tried explaining this as easily as I could, let me know if you have any more questions :)
     
    Last edited: Jan 18, 2019
  3. jameslol417

    jameslol417

    Joined:
    Jan 18, 2019
    Posts:
    7
    thank you for the help! :D