Getting variable from another script

Hey, I’m having trouble getting a variable from another script. This is my code:

private var network : GameObject;
private var network1;

network = GameObject.Find("NetworkController");
network1 = network.GetComponent(ChatController);

function Update () {

if (network1.typingMessage) {

	GetComponent(CharacterMotor).enabled = false;
	GetComponent(CameraSwitch).enabled = false;
	GetComponent(Noclip).enabled = false;

}

if (!network1.typingMessage) {

	GetComponent(CameraSwitch).enabled = true;
	
			if (GetComponent(Noclip).on) {
			GetComponent(Noclip).enabled = true;
			}
		
			if (!GetComponent(Noclip).on) {
			GetComponent(CharacterMotor).enabled = true;
			}

}

}

What it does is it gets a GameObject called “NetworkController”, and then gets the script called “ChatController” attached to it.

Then in the Update function it checks to see if a variable from the “ChatController” script called “typingMessage” is true, and if it is, it disables 3 scripts.

Then in the next if statement, it checks to see if the “typingMessage” variable is false, and if it is, it enables the “CameraSwitch” script. Then it only enables the Noclip script if the “on” variable from the Noclip script is true, and it enables the CharacterMotor script if the “on” variable is false.

The CharacterMotor, CameraSwitch, Noclip, and this script are all on the same object.

If anyone could tell me what I’m doing wrong, that would be great…

While you haven’t told us exactly what error messages you are getting, there are a few things you could do straight off to improve the code here.

1: Move all the stuff that happens ‘outside of any functions’ into an explicit ‘Start’ function. It looks nicer.

2: Staticly type network1- make sure that it knows what it is supposed to be from the moment it is declared.

private var network1 : ChatController;

3: Instead of using GameObject.Find(string), just make network public and assign it in the editor! This way you don’t have to worry about spelling errors in your hardcoded script.

4: You are calling GetComponent up to 6 times every frame here. GetComponent is quite a slow function, and it would be better just to do it once and remember the results. In your new ‘Start’ function, get all the values you would retrieve in that way, and then substitute that into all the places you would have used them in Update.

var network : GameObject;
private var network1 : ChatController;
private var motor : CharacterMotor;
private var switch : CameraSwitch;
private var noclip : Noclip;

function Start()
{
    network1 = network.GetComponent(ChatController);
    motor = GetComponent(CharacterMotor);
    switch = GetComponent(CameraSwitch);
    noclip = GetComponent(Noclip);
}

5: You seem to be checking every frame for if the chatController is typing, and then resetting every value every frame, even if it hasn’t changed. There are a few fixes for this- first, you could keep a boolean which it first checks the current value of network1.typingMessage against, and if they are not the same it executes the rest of the function. You could change it all to a callback model, where the chatController has a reference to this script, and tells it whenever it starts or stops typing.

If you do all of that, see if your problems persist. Otherwise, post a comment with more details about exactly what is going wrong with your script!

I found out why it wasn’t working, I forgot to include two quotation marks here:

network1 = network.GetComponent(ChatController);

It now looks like this:

network1 = network.GetComponent("ChatController");

But thanks for your help, I’m kind of new to scripting and your advice really helped me clean up my code.