Access Variable from Different Object, Different Script

Not sure why this is so hard to figure out. I’ve looked through questions/answers/videos/documentation… something this simple is taking forever. Makes me want to forget it and buy PlayMaker. Half of the answers on here feature an annoyed programmer saying the answer is already out there… Enough with the prologue. I’m trying to change the value of a Vector3 in a script from a different object, I’m using C#.

public class Track_Long_Script : MonoBehaviour {
	// var GeneratePointScriptVar;
	// private GameObject DaGenerator;

	void Start () {


		// DaGenerator = GameObject.Find("GeneratePoint");
		// DaGenerator.GetComponent(GeneratePointScript).GenerationPoint +=1;

		// GeneratePoint.GetComponent(GeneratePointScript).GenerationPoint +=1;

		// GeneratePointScriptVar = GeneratePoint.GetComponent<GeneratePointScript>();
		// GeneratePointScriptVar.GenerationPoint = 0;

		// DaGenerator = GameObject.Find("GeneratePoint");
		// DaGenerator.transform.Translate(0,-20,60);


Sorry for the code that doesn’t make any sense… I don’t know what I’m doing wrong. Just trying a bunch of things that might work. Hoping someone with a kind heart will see somebody who is pulling all nighters to do simple things!

I just want to have a script that will translate the transform of a vector 3 in a different object’s script every time it’s called.

Thank you in advance, kind person!

Half of the answers on here feature an
annoyed programmer saying the answer
is already out there

There are video tutorials, pages in the Unity manual, pages in Unity Gems, a bizillion answer on UA, blog posts, and answers on Unity Forums, all focused on this topic. This question still comes up in some form multiple times on most days, and most of those questions give no indication that the person asking the question did what you did and attempted to answer their own question through research. So yea, we do get annoyed.

First there is a concept here that you absolutely have to understand. People talk about accessing other scripts. But since most scripts can be on multiple game objects, what you are really trying to do is get access to some specific instance of that script attached to some specific game object. Unity refers to these instances as ‘components’. So getting access to a variable in another script is a two step process. First find the game object that has the script, then get the component you are seeking. Sometimes the find-the-game-object step is hidden because Unity is handling it under the hood.

There are multiple common ways to get access. I’m going to outline one. It works when the component exists when the script trying to access it exists during Start(). So at the top of the file, create a class instance variable:

 private GeneratePointScript gps;

Then in Start();

 gps = GameObject.Find("NameOfTheGameObjectWithScript").GetComponent<GeneratePointScript>();

Then you can use it to access anything public in that class. What you are accessing, method or variable, must be public. For example:

Vector3 v = gps.myVector;

Note it is initialized once in start so that the GameObject.Find() is not executed every frame. If you are only going to be executing occasionally, you can do:

 Vector3 v = GameObject.Find("NameOfGameObjectWithScript").GetConmponent<GeneratePointScript>().myVector;

Also there are more efficient ways of finding the game object. For example, if you tag your game object, you can use GameObject.FindWithTag(), which is much more efficient.

Since you asked nicely here is some annotated code

// This is the script we want to access
public class OtherScript : MonoBehaviour {    
    // Variable to access must be public
    public Vector3 myVariable;    

public class AccessingClass : MonoBehaviour {
    // The simplest way is simply to use a public variable
    // Assign this by dragging in from the inspector
    public OtherScript otherScript;
    void Start () {
            // Set the variable like so
            otherScript.myVariable = new Vector3(1,1,1);    
            // The next way is to use GetComponent
            // This only works if the OtherScript is on the same GameObject
            otherScript = gameObject.GetComponent<OtherScript>();
            otherScript.myVariable = new Vector3(1,1,1);
            // Finally you can use Find to get a different GameObject by name
            GameObject other = GameObject.Find("Other");
            otherScript = other.GetComponent<OtherScript>();
            otherScript.myVariable = new Vector3(1,1,1);
            // And there is nothing stopping you doing it all in one line
            GameObject.Find("Other").GetComponent<OtherScript>().myVariable = new Vector3(1,1,1);

Try using this between scripts:

static variables