OtherScript?

I found this example about how to set a variable in another script that you assign in the inspector:

// Set foo DoSomething on the target variable assigned in the inspector.

var target : OtherScript;

function Update () {
    // Set foo variable of the target object
    target.foo = 2;
    // Call do something on the target
    target.DoSomething("Hello");
}

However, I am told

Has this changed, what it going on?

That example assumes you also have another script named OtherScript.js, in which you’ve defined the DoSomething function.

I think thats because your script was created on other version of Unity or ur project was crashed. Do this steps:

  1. Copy all from the "OtherScript you want to acces
    2.Delete this script
    3.Create new and name it same as the previous script was named
    4.Paste the text you have copied (Save it).
    5.Now the error should dessapear.

Hmm… then I don’t understand why they write “assigned in the inspector” if it’s supposed to mean a script that you write by name…

I really wanted to refference the script in the same way as you do with an object using:

var target : Transfrom;

Is this not possible with scripts?
I keep reading that top info line as meaning just that…
When I try writing the name of the script in the way you say though, I get "Cannot cast from source type to destination type. "
Anyways, for now I have a work around, that works!

Sure it is…

var target : Transform;

function Update(){
	var otherVar : OtherScript = target.gameObject.GetComponent(OtherScript);
	if( otherVar ){
		// Set foo variable of the target object
		otherVar.foo = 2;
		// Call do something on the target
		otherVar.DoSomething("Hello");
	}
}

When referencing an objects script, when you only have the object, you get the script component. And in the case of what I posted, it also verifies that the script is available for use.

If you have a script named OtherScript.js in your project folder, that by default creates a monobehavior that unity will recognize as OtherScript, and you can type objects as such. So you could then declare a public variable typed as OtherScript like:

var target : OtherScript;

Now since target is explicitly typed as OtherScript, the only thing you can drag to the “target” field in the inspector is a GameObject in the scene that has the OtherScript.js component attached to it (or the OtherScript component attached to the same object as this code), and the variable will become a reference to that object’s instance of the OtherScript component.

Like in your “target : Transform” example… how do you usually use that? You drag a GameObject from the scene into that variable, and then that variable becomes a reference to that GameObject’s transform component. You don’t drag the “Transform” script.

Ahh… okay, yes, now I see.
It was indeed the “drag to the target field” I was interested in.
It does indeed work now doing it like that, and makes, off course, a lot of sense that you still need to refference a GameObject having an instance of that script, and not just the script floating around un-attached somewhere, which was what was going on in my mind I guess…

I ended up combining the three scripts into a bigger one, because it started to make more sense doing so, but it’s good to know how it works, thanks!