Can't read or write to other scripts

I just do not get this scripting language. I’ve read the documentation countless times and nothing seems to work.

I have a scene, there are two cubes in it. Lets call them cube1 and cube2. Both of these cubes have scripts attached to them which store variables, let’s call them script1 for cube1, and script2 for cube2.

Now, Cube2 moves along the Y axis, and when it goes beyond the value of 5, it has to modify a variable called “stuff” in script1. (I have no issue moving the object along the Y axis, that’s not my problem).

Everything I’ve read on forums and the documentation keeps telling me that to change a variable on another script :

script1.stuff = 2;

will modify the variable “stuff” in script1, which is attached to cube1.

However, this gives me a compile time error : BCE0005: Unknown identifier: script1.

So that’s fine, I figure I’ll go read the examples in the Unity script reference. They say :

otherScript = GetComponent("script1");
otherScript.stuff = 2;

That gives me a new error : Object reference not set to an instance of an object.

So I try all sorts of crazy things like “new”, but I just can’t get past something so simple.

Help! There’s ummm… gratitude and thanks in it for you? :slight_smile:

The code below is for cube 2 so it can alter the variable in cube1 when cube2 passes 5.0 in Y axis;

var cube1Script : Cube1 

//This has to be the same with the script name. All script names are also class names in Unity's Jscript so if it's name
//is Cube1Manager.js you have to write Cube1Manager instead of my Cube1
//Now you can set your script in the inspector window but I will find this script within this script

function Start(){
    cube1Script = GameObject.Find("YourCube1GameObjectName").GetComponent(Cube1)
    //This is fully optional, what it does is, it finds your cube objects script named Cube1 as soon as you run
    //the application so you won't have to attach it in inspector
}

function Update(){
    if(transform.position.y > 5.0){
        cube1Script.stuff = 10;
    }
}

Thanks for the code example!

I’ve tried that but I am getting, “The name ‘cube1’ does not denote a valid type (‘not found’)”.

So I figured maybe you meant that I have to give the script the same name of the object. Done, but still results in the same error.

var cube1script : Cube1;

Cube1 is the script name / component, he could have written Transform or GameObject instead, for example.

Write the name of the script which contains the variable stuff.

Ahhh ok - it looks like my project has become corrupted somehow. The code you provided just will not work in this project, but if I put it into another project, it works just fine.

Thanks for everyone’s help!

EDIT:

I tell a lie. I am back at the “Object reference not set to an instance of an object” error. Based on what my original problem was, and the solutions presented to me, the script looks like this:

var cube1Script : script1;

function OnMouseDown () {
	cube1Script = GameObject.Find("Cube1").GetComponent("Script1");
	cube1Script.stuff = 2;
}

Cube1 is the object, Script1 is the script. stuff is the variable. The error occurs when I try to set the variable, “stuff”. I’ve taken out the Y movement condition and changed it simply to when the user clicks on cube2, it should change the variable on cube1’s script1 script.

What is wrong with that script? Seems fine to me!

EDIT (again) :

Just realised my typo, sorry! Script is fine - I’m just an idiot.