My Light? Where's my light? ^^

I just started working with Unity and got a little “newbie” problem: in the editor, I have a light named “FireLight” that should be disabled at game start. To deactivate it on game start (and to animate it), I attached the following Script to the light object:

var MyLight:Light;

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

function Update () 
	{
	if (MyLight.gameObject.active)
		{
		MyLight.intensity = Random.Range(0.0,1.0);
		}
	}
	
	

@script RequireComponent (GameObject)

When the player touches a trigger, I want to activate it by script. The problem is that I am not sure how I should reference the light object from within the player script. Using this does not work and results in a “NullReferenceException” error:

function StartFire()
    {
	var MyLight:GameObject = GameObject.Find("FireLight");
	MyLight.gameObject.active = true;
    }

Did you drag the Light GameObject to the scripts MyLight variable in the Inspector?

Easy to forget, and I usually check variables for null at the start.

If the script that turns the light off is on the object that gets made inactive, you’ll never find it. Inactive objects cannot be found by name or by tag; you need a direct reference.

Just store a link to the light in the script that reactivates it and then it’s simple. myLight.gameObject.active = true;

I also tried to access the light without deactivating it at game start, but it still cannot be found (using the method from my first post). That really confuses me. I am able to find any object by name or tag, but not the light. I don’t know why.

How should I store a direct reference to the light in any script that needs to access it? I don’t like the method of dragging objects explicitly onto a script in the editor, especially when there are houndreds of objects in the scene. This would be quite inconvenient :frowning:

sigh If game objects could be declared as global to be accessable from anywhere, it really would be much easier…

GameObject.Find
If no game object with name can be found, null is returned. This function only returns active gameobjects.

The is nothing to stop you keeping a reference of every GameObject in the scene, so you don’t have to look for them.

Drag/Drop in the Inspector has its uses, especially pre-defined things like prefabs. It also links dependencies in, when it comes to the build.

Not sure why you can’t find “FireLight”. My best bet is that the Start() ran in the Light script first and made it inactive, so you couldn’t find it.

Okay, since everything else failed, I now added two functions to the Script that is attached to the light object (which is named “AnimateFireLight”): function StartFire() and function StopFire().

Now I try to access those functions in the light script from the script attached to the player like this:

AnimateFireLight.StartFire();

This results in an error message "An instance of type “AnimateFireLight” is required to access non static member “StartFire”.

How can I create such an instance of the script? Isn’t it enough that the light script has been already attached to the light object for Unity to globally “know” it? I am getting more and more confused with Unity’s script / variable / function scopes and how to access them among each other :frowning:

I’ve been doing it like this, only been using Unity a few days so maybe it isn’t the best way.

var someVariable : ;

Drag your gameObject that has the included script attached.

Now you can access the same variables from a different script by doing:

someVariable. = whatever;

or

someVariable.;

Another way is to do this:

function OnTriggerEnter (other : collider)
{
other.gameObject.SendMessage(“someString”, SendMessageOptions.Don’tRequireReceiver);
}

Now anything can be put in that “someString” spot and when something collides with the trigger it will search the object that collided with it for a function by the same name as “someString”. If one exists it will run your code, if one doesn’t exist it will do nothing.

Thanks Zero, I tried your first method and it works -although I tried to avoid that because I’d like to be able to access everything purely by code only, not by “dragging things onto other things” in the editor. Thid does not feel right for me :slight_smile:

After testing around a little bit, I assume that with Unity a script can only access another script if both scripts are attached to the same object. Is this correct?