how do I access variable or call a function from other script in unity

 Accessing and modifying variable and calling from other script is quit simple. So in this post we access variable from other script and modify and call it. so lets get started. 
  • Lets create an empty scene in your project.
  • In this scene create two empty game object and named them "variable and function script" and "test  script".
  • Now create two scripts one in which we declare variable and function named as "variable_function" and second by which we access these variable and call function named as "test". 
  • Drag and drop variable_function script in "variable and function script" gameObject and test script in "test script" gameObject.
  • You can use any name of gameObject and script these names are just for reference so you can understand which gameObject and scripts I am talking about.

variable_function script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class variable_function : MonoBehaviour
{
    public int test_var=0;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void test_fuction()
    {
        Debug.Log("test fuction run and test variable value is " + test_var);
    }
}


  • in this script we declare a public variable of type int named "test_var" and intialized as 0 for our post. You can declare or use any data type like bool, float, string etc.
  • secondly in this script we declare a public function named "test_function" of void return type without accepting any argument. 
  • Debug.Log("test fuction run and test variable value is " + test_var); Debug.Log show message in "test function run and test variable value is" and  + test_var show the value of test_var.    
  • Always make variable or function public when you want to access them from outside of the script (from other script). 

That's all are variable_function script contained now lets take a look at test script

test script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class test : MonoBehaviour
{
    public variable_function my_script;
    // Start is called before the first frame update
    void Start()
    {
        my_script.test_var=1;
        my_script.test_fuction();

    }

    // Update is called once per frame
    void Update()
    {
        
    }

   
}


  • First we have to drop reference to the to variable_function script so create a variable of data type variable_function named as my_script  public variable_function my_script
  • my_script.test_var=1  following code will change the value of test_var to 1. for access a variable of different script you can use refencenced variable of script type then . (Dot) and then name of variable. my_script.test_var this will behave a variable and you can then perform any operation on it. this code will directly refence to test_var of variable_function script any changes in it are changes on test_var or vice versa.
  • my_script.test_fuction(); following code run test_function in variable_function script. 
  • Now drag "variable and function script" from hierarchy to my_script in inspector window.
  • We place both code in start method so when we hit play button the value of test_var is 0 set at initialization then start method will run and by first code line it change value from 0 to 1. and then second line code run and show a message in the console "test function run and test variable value is 1" .    
 
accessing variable by script in unity


Above all code are example how can you access. You can use them anywhere or any number of variables in and script but both scripts should be in scene view and there are other methods of accessing a variable script. You can do same task by declaring gameObject and access script by GetComponent method instead of direct access.

After referencing variable script you can use .(Dot) and name of variable.
 

Popular posts from this blog

How to fix Android Gradle Build failed error in unity

Changing position of gameObjects in unity by C# script

WaitForSeconds and coroutine in unity | delaying a code action for some time in C#

Unity ads install error from package manager

SerializeField and HideInInspector in unity in C# script

playing audio in unity by C# script

how to setup unity ads in mobile game

GetComponent method in unity to access component by C# script

Variables in C# script in unity

Activating and deactivating gameObjects in unity by C# script