Trigger Happy

Hey Everyone

I’m attempting to add a script to my First Person Controller that will make an object named Acorn dissapear when the player is next to it and presses “Fire1.”

The script that I have tried is:

var acorn : Transform;

function OnTriggerStay(other : Collider) {
	if (Input.GetButton("Fire1"))
		Destroy (acorn);
}

When I place a box collider and this script on the First Person Controller and define the target as Acorn, I get an error that says “Can’t Remove Component” (even when nothing is in the trigger collider).

I get the same error when I place the box collider and script on the Graphic child of the First Person Controller.

If anyone has a suggestion or needs more clarification please let me know.

Thanks

When you say Destroy(acorn), acorn is referncing the transform of the acorn. Transform is a component of a GameObject - a special, non-removable component, but a component. So you need to rather destroy the GameObject associated with the transform, using Destroy(acorn.gameObject). Or, reference the gameObject instead of the transform when you declare the variable.

Thanks for such a quick response. That was a pleasant surprise.

I’ve changed the script to this:

var acorn : GameObject;

function OnTriggerStay(other : Collider) {
	if (Input.GetButton("Fire1"))
		Destroy(acorn);
}

The object named acorn disappears now. But it seems to have uncovered another problem. The acorn disappears whenever Fire1 is pressed, not just when the acorn is in front of the Controller.

This is because the Graphic that is the Child of the First Person Controller is within the box collider trigger and therefore triggering the script. (see attached Picturea.png)

However when I adjust the box collider trigger so that it is not intersecting with the Graphic (see attached Pictureb.png), the acorn no longer disappears when Fire1 is pressed.

How can I script this so the acorn only disappears when the object named acorn is within the Controller’s box collider?

[/img]

51448--1881--$pictureaandb_237.png

function OnTriggerStay(acorn : Collider)
{
 blah blah blah...then
Destroy(acorn.gameObject);
}

see if that works

Ray

Ray,

This is how I interpreted your suggestion:

var acorn : GameObject;

function OnTriggerStay(acorn : Collider) {
	if (Input.GetButton("Fire1"))
		Destroy(acorn.gameObject);
}

I attached this script to the First Person Controller, but for some reason this made the First Person Controller itself disappear but leaves the acorn alone, which really confuses me.

Reference your game object in the inspector to acorn game object.

Image below has a target : GameObject;
that will be destroyed upon entering the trigger

Edit:
Not sure if I understand it completely but,

Try to attached the script to the box collider trigger, since that’s the one that should cause the acorn to disappear?

Edit again

var acorn : GameObject;

function OnTriggerStay(acorn : Collider) { 
   if (Input.GetButton("Fire1")  acorn.collider) 
      Destroy(acorn.gameObject); 
}

add acorn .collider to if(input.GetButton(“Fire1”)

Sorry I’m a newb, hope that works.

51453--1882--$sample_191.jpg

You could use the Bounds.Contains function to identify if your Character Controller’s bounding box is totally within the trigger area. You would have to create a Bounds using the width and height of your trigger and then test against two opposite corners of your Controller.

Or you could just make your trigger smaller so that the event is passed when you’re closer to where you want it to be?

Thank you soo much. It works just like you said with no tweaking needed.