Reference Null Error with Collider

I have a "first person controller" object with a "weaponsmanagemnt" script and a child object: "Graphics."

graphics has a capsule collider on it.

another object in the world has a "triggerme" script on it which I want to reference weaponsmanagement when I walk onto it. I'm using this code but its giving me a NullReferenceException error on the line after the if statement:

function OnTriggerEnter (other : Collider) 
{
    if (other.gameObject.tag == "Player"){
        other.gameObject.GetComponent("WeaponsManagement").showGui();
    }
}

the weird thing is, even though it gives me that error: "object reference not set to an instance of an object" it still seems to call the function showGui.

also, I've tried other things such as "other.gameObject.transform.parent.GetComponent" and the like. I get the same error. How do I call the script on it's parents GameObject if all I have is a collider to work with?

2 Answers

2

GetComponent returns a component, not a WeaponsManagement object. you will need to cast that unless you are on unity 2.x

also I would recommend to never use strings for getcomponent, use the classname without the string. that way the compiler will inform you on typos

just took off player tag from graphics and it was ok