getComponent not working with Triggers?

I have the following JavaScript code:

//Spin every frame
function Update () {
	this.transform.Rotate(0,2,0);
}

//Increases the bubble count
function MoreBubbles (){
    var other : screenGUI
	other = gameObject.GetComponent("screenGUI");
	other.bubbles += 1;
}

//Dissapear on trigger with anything
function OnTriggerEnter (triggerInfo : Collider) {
	MoreBubbles();
	Destroy(gameObject);
}

It’s meant to be a bubble, that whenever it touches the player, it accesses a variable in a script on the main camera (Being the script “screenGUI” and the variable “bubbles”) However, whenever the player touches the bubble, i get the following error:

NullReferenceException: Object reference not set to an instance of an object
BubbleScript.MoreBubbles () (at Assets/Scripts/BubbleScript.js:10)
BubbleScript.OnTriggerEnter (UnityEngine.Collider triggerInfo) (at Assets/Scripts/BubbleScript.js:15)

Checked the code comparing it with a few examples on the scripting reference manual. Still get the same error or others. Anyone mind helping?

EDIT: I found a few minutes afterwards that i can do this using a global variable, but i still want to figure out this method for other things :slight_smile:

You need a reference to the object to which the script screenGUI is attached, or else GetComponent will return null, and other.bubbles will give this error. If this object is the main camera, you can use:

function MoreBubbles (){
    var other : screenGUI
    other = Camera.main.GetComponent("screenGUI");
    other.bubbles += 1;
}