Stupid Question 10394: Referencing another script

OK, this is driving me absolutely bananas.

I’ve spent hours now reading dozens of doc pages, answers to other questions on UnityAnswers and here and blogs, etc etc etc, and nothing is working. I must be missing something really basic.

To be clear for future generations, I’m going to say that:

  • I have a script called PlayerUpdate with a boolean variable called isDead
  • I have a script called EnemyUpdate which needs to access that isDead bool
  • PlayerUpdate is attached to an object called Player
  • EnemyUpdate is attached to an object called Enemy
  • I’m doing this in JavaScript

The two methods most docs talk about are to add the following to EnemyUpdate:

var playerScript : PlayerUpdate; in definition

  • or *
var playerScript;

function Start()
{
    playerScript = transform.Find("Player").GetComponent(PlayerUpdate);
}

What I get when I try either one the compiler simply says, “The name ‘PlayerUpdate’ does not denote a valid type.”

Coming from a strong programming background, I’d expect this error because scripts aren’t classes… are they?

Totally baffled, and hoping for some clarity. Thanks to anyone who can help.

var playerScript : PlayerUpdate;

function Awake () {
	playerScript = GameObject.Find ("Player").GetComponent (PlayerUpdate);
}

Be careful, transform.Find looks for a CHILD. GameObject.Find looks for a gameObject, whether it is child or not.

I’ll add that you must type

GetComponent (“PlayerUpdate”); instead of
GetComponent (PlayerUpdate);

(Or put PlayerUpdate in Standard assets / Pro assets / Plugins folder, Unity - Scripting API: )

Egads, I should have asked here earlier. It was the combination of errors that made all my attempts pointless. I’ll check out that option for Assets/Plugins for future use.

Thanks so much to both of you! What a relief to have it working.

I have always written GetComponent (Transform) , (FPSPlayer), and (PlayerUpdate), it worked ?

@ Grondhamma: Glad it worked :slight_smile:

@ AkilaeTribe: Unfortunaly, I get the same error while compiling (“The name ‘ScriptName’ does not denote a valid type.”) if I don’t write this. Are you using JS?

Per the documentation on GetComponent, you can use GetComponent with a type (and preferred for performance reasons. In JS, this is as simple as myComponent.GetComponent(MyClass), but in C#, you need to use either myComponent.GetComponent(typeof(MyClass)) or the generic version myComponent.GetComponent<MyClass> ()
Also remember that when not using the generic version, you’ll need to typecast the result to use it as a MyClass object (otherwise it’s just a Component).

Show us the code ?

@Tribe

I think Alex is referring to the code he posted:

GetComponent ("PlayerUpdate");

instead of

GetComponent (PlayerUpdate);

…which was a crucial fix for me, at least (using the former rather than the latter).

And just so this conversation doesn’t get muddied, my question was not about accessing a class, but a script. I think this is why finding an answer is so difficult — most people in Unity-land seem not to differentiate between the two very cleanly.

Is this because the C# folks have to package every script as a class?

Javascript scripts are classes. The syntax obfuscates it a bit, but a Javascript script named MyScript can be thought of as having an invisible

class MyScript extends MonoBehaviour {
    //Your script contents
}

around it. This page gives a decent overview of what’s going on. From that page,

Yes, you’re right

Then why does (in my example) GetComponent(PlayerUpdate) blow up? I believe you, I just don’t understand the behaviour I’m seeing.

There are a couple reasons I can think of off the top of my head why you’d get that error. The easy one is to check if the file name for PlayerUpdate is, in fact, “PlayerUpdate.js.” Another reason might be if the script referencing PlayerUpdate is in the standard assets folder (or PlayerUpdate is in C# and isn’t in the standard assets folder). Make sure you’re following the rules on compilation order here. Just from copying your code, the compiler error I get actually comes from the line where you declare var playerUpdate : PlayerUpdate; rather than the GetComponent line. If you use the string version of GetComponent, does it work as expected at runtime or are you getting null reference exceptions when you try to do anything with playerUpdate?