[Help] how do I reference/access another script in unity C#

I am making a text adventure game, but I don’t know how to sync variables from one script to another also please give a good explanation on how to do it.

First thing’s first. Computers can not make assumptions, so we must define everything within in the computers abilities.

Almost everything that can be referenced follow the same basic syntax in C#.

//[scope] [type] [name]

	public float velocity = 10f;

	private int money = 10;

	protected string dairy = "cheese";

	public float Velocity { get{} set{} }

	private int Money { get{} set{} }

	protected string Dairy { get{} set{} }

	public void Velocity () {}

	private int Money () {}
	protected string Dairy () {}

Everything listed here is interchangeable excluding the syntax. What you want to focus on first of all is the scope which defines where your Method, Variable, or Property can be accessed.


  • public - Can be accessed by any and all scripts just as long as you have a reference to the script where the variable, property, or method is created.

  • protected - Can be accessed ONLY by inherited members, a good example of inheritance is:

    public class MyScript : MonoBehaviour {


Almost all of your game scripts in Unity will inherit MonoBehaviour allowing you to use everything MonoBehaviour owns, however. If you didn’t inherit it, it won’t be possible to interact with your game as many of the Methods, Variables, and properties are protected. Like only you and the people living in your home have access to the attic.

  • private - Can not be accessed from anywhere, other than the class it was created in. Period.

Now that we got that squared away, creating a reference is quite easy, and there’s a bunch of ways to do it.

If you’re trying to reference a script that’s attached to the same GameObject that you’re referencing from all you have to do is GetComponent.

//Creating and assigning the reference variable.

ReferencedScript refScript = GetComponent<ReferencedScript>();

//Creating then assigning the reference variable.

ReferencedScript refScript;

refScript = GetComponent<ReferencedScript>();

If you’re referencing a script from a different GameObject, you much reference the GameObject first, then reference the script.

//Without reference variable
GameObject objectWithScript;


//With reference variable
GameObject objectWithScript;


Once you have referenced your script, you can access anything within it that’s been set to public. Even assign variables and properties from your the script you’re referencing from or call methods.

//From Same GameObject
GetComponent<ReferencedScript>().velocity = 4f;

refScript.Velocity = 5f;


//From Seperate GameObject
objectWithScript.GetComponent<ReferencedScript>().velocity = 4f;

objectWithScript.refScript.Velocity = 5f;


yes you could just attach that script to that gameObject and also I do believe u can make that script static just get access to that script (In the same script) and make it static and by looking at your code i believe you know what a static variable is and yeah