Calling to another script is causing a "null" result

Hi everyone,

I’ve been going on for day about this problem, I can’t seem to fix it. I want buttons In my game to call for action to my char. So I’ve got a script where an action is set, he should call to another script to activate the fucntion. But it is giving me a null result. Code:

  function Start () {

    	var otherScript: Background = GetComponent(Background); 
    	if(otherScript == null) {
    	else {

otherscript(background) :

function black () {
	renderer.material.color = Color(0.0, 0.0, 0.0);


I’ve built in a method to see if the code is null, otherwise I would get a Null pointer exeption.

the method is not called, because it’s null. But I don’t know how to fix it.

Thanks in advance,

The problem is that you need an Instance of the class Background. You are just calling for a class called Background on this scripts object.

Imagine you wanted to access a BoxCollider on another object. And you just called GetComponent(BoxCollider). It wouldn’t know that you wanted to call THAT specific object’s boxcollider and returns the BoxCollider on this object.

In your case it doesn’t find it, so it returns null.

You should create a variable in your script and link it in the inspector. If this is not possible you need to look for it somehow, maybe in some array/list or something.