Java Scripting Basics: Communicating Between Scripts

In studying scripting I’m trying to work out some issues I’ve been having with accessing components/objects/variables between different scripts.

For testing purposes I created a new project, and created only the following: A Sphere, A GUITexture (left at the default unity watermark), and two scripts.

I’m still very new to scripting, and my syntax is all over the map. While the Unity API has been very helpful, it’s coding examples (when supplied) are rather weak.

This script is attached to the Sphere:

function Update () {
	
 if(Input.GetButtonDown("Fire2")){		
  GameObject.Find("TestTexture")
  .gameObject.enabled=true;
	
 }

}

This script is attached to the GUITexture:

gameObject.enabled=false;

function Update () {
}

This generates two error codes, or rather the same error appearing once for each script:

‘enabled’ is not a member of ‘UnityEngine.GameObject’

Then I rewrote the code as follows:
Sphere

function Update () {
 if(Input.GetButtonDown("Fire2")){
 GameObject.Find("TestTexture").enabled=true;
	
 }

}

GUITexture:

enabled=false;

function Update () {
}

This provides the same error, but only on the script attached to the Sphere object.

Then as follows:
Sphere:

function Update () {
	if(Input.GetButtonDown("Fire2")){
	GetComponent(TestTex).enabled=true;
	
	}

}

This provides the error: ‘Object reference not set to an instance of an object.’

Can anyone tell me what the proper syntax for this code would be? Thank you for your time.
[/code]

GameObject.Find(“TestTexture”).enabled=true;

this will only work inside a function

basicly it’s :

GameObject.Find("myObject").enabled = true/false;
GameObject.Find("myObject").GetComponent(myscriptname).mysetting = myvalue;

GameObjects can’t be enabled/disabled, that’s only for components. GameObjects can be active or not.

–Eric

I have now tried the following:

Sphere

function Update () {

 if(Input.GetButtonDown("Fire2")){
 GameObject.Find("TestTexture").active=true;
	
 }

}

GUITexture

function Start(){
gameObject.active=false;
}

function Update () {
}

This causes the error:

Null Reference Exception
GUITrigger.Update() (atAssets\StandardAssets\Scripts\GUITrigger.js:4)

in reference to the line GameObject.Find(“TestTexture”).active=true;

I’ve also tried it with simply ‘active=false’, leaving out gameObject. I’ve never really found it clear when to use ‘gameObject’ and when it is not needed.

If you are similar with OOP “gameObject” is same as “this” reserved word, I tested and “this” reserved word and it works as well.

It’s not finding an object named exactly “TestTexture”.

GameObject is the class and is used for calling static functions ( like Find )

x.gameObject is a reference to the GameObject part of the Component x, which is often this.

I’m sorry, but I’m not really following. Could one of you please post an example of the code? As I said I’m in the process of learning the basics of scripting.

I’ve also tried using a static variable of type GUITexture, as per the following code:

GUITexture

static var texElement : GUITexture;

function Update () {
}

Sphere

function Update () {
	if(Input.GetButtonDown("Fire2")){
		texElement.enabled=true;
	}
	
}

It returns the error ‘unknown identifier texElement’. Once again I need help understanding some basic concepts. It was my understanding that the ‘static’ keyword made variables accessible between scripts. Is there another keyword I should be using?

GameObject.Find(“TheObjectsName”).GetComponent().VariableToChange = myvariable;
or in js:
GameObject.Find(“TheObjectsName”).GetComponent(“TheScriptIWant”).VariableToChange = myvariable;

When using statics you must include the script name as the space name.
It’s the same concept as using Mathf.Min() instead of Min(); the compiler needs to know where to look for variables/functions that aren’t in the current script.

eg

Game.js

static var updates : int = 0;
Other.js

function Update() {
 Game.updates += 1;
}

Also

GetComponent(ScriptName)
is probably fasterbetter than
GetComponent(“ScriptName”)

Although the string version does have its uses (when you want to programmatically decide which component to get, for example).