Accessing parent game object and storing as a variable

I’m trying to get an object’s animation to play if a box collider trigger, which is the child of said object, gets hit by the player. Try as I might, I can’t successfully assign the parent object to my variable without using GameObject.Find, which I don’t want to use. I’ve been trying to use transform.parent.gameObject, but my console just keeps telling me that the variable is unassigned.

function OnTriggerEnter (collider : Collider)
{
	var treasureChest : GameObject = transform.parent.gamObject;
	treasureChest.animation.Play();
}

Here’s my code.

The objects in question are a treasure chest (the parent) with an animation which I want to play, and a child box collider, to which this trigger script is attached.

gamObject != gameObject

hi, you can use the collider children

in this method(c#):

 GameObject sample = obj.collider.gameObject.transform.parent.parent.gameObject

Debug name:

Debug.Log("GameObject Parent: "+ sample.name);

in Java, i think so:

function OnTriggerEnter (collider : Collider)

{

    var treasureChest : GameObject =  collider.collider.gameObject.transform.parent.parent.gameObject;

    treasureChest.animation.Play();

}

Why do you have two GameObject parented to each other?

Sounds like exactly what you would want to do , so that the trigger volume moves with the chest object.

I … whu …

Argh. Thank you for your patience.

Why is the trigger volume (component) not on the chest GameObject?