Trigger not Working

var target : Transform;
var raisedHeight = 5.5;
private var startHeight : float;

function Start () {
startHeight = target.transform.position.y;
}

function OnTriggerEnter (other : Collider) {
if(other.gameObject.tag == ("Player" || "Crate")){
	target.transform.position.y = raisedHeight;
}
}

function OnTriggerExit (other : Collider) {
if(other.gameObject.tag == ("Player" || "Crate")){
	target.transform.position.y = startHeight;
}
}

This is for a button that lifts a door. Works fine with the player, but not with the crate. Everything is tagged correctly.

Any ideas?

This is probably not the right answer. But what happens when you put a rigid body onto the crate, set its mass to 0 and turn use gravity off?

IsTrigger checked?

It already has a rigidbody, and I need it to use gravity.

Yes. As i said it works fine with the player.

Okay just trying to help… Is the size of your collider big enough so that they are intersecting?

Have you tried using other.tag instead of other.gameObject.tag? The example I learned from doesn’t include gameObject. Maybe the gameObject of the crate isn’t tagged but something lower in the hierarchy is.

the crate is literally a cube with a rigidbody attached. it is what is tagged.

Have to ask the obvious questions since its not working…

I understand.

You know that a Collider that is set as a trigger doesn’t respond to gravity right, or any physics for that matter.

I meant the crate, not the trigger.

Sorry for the confusion.

I know you’re talking about the crate… What’s your object hierarchy?

You have a few contradicting statements here…

  1. You say you want your crate to respond to gravity, so it has a rigidbody, and I assume a box collider. Thats fine, but non-trigger colliders dont respond to trigger events, only collision.

  2. You dont want it to fall through the ground, so it cant be a trigger, unless you have setup a compound collider system, but since you said its a crate with a rigidbody, I doubt this.

I’m confused. if it has to be a trigger to work, than this is completely useless because it needs to use collision for my purposes.

Also, the player is not a trigger, so the crate should not have to be either, if im understanding correctly.

Correct.

Easy fix is to use Collision event… its pretty similiar to OnTriggerEnter…

But I dont want to use a collision event. I dont want it to actually look like its hitting anything. I dont understand why it works with the player but not the crate.

I suggest you go and read this page.

http://docs.unity3d.com/Documentation/Manual/Physics.html

what you want can be done.

Ok, well this does nothing whatsoever but return errors:

var target : Transform;
var raisedHeight = 5.5;
private var startHeight : float;

function Start () {
startHeight = target.transform.position.y;
}

function OnCollisionEnter (Collision : Collider) {
if(Collision.gameObject.tag == ("Player" || "Crate")){
	target.transform.position.y = raisedHeight;
}
}

function OnCollisionExit (Collision : Collider) {
if(Collision.gameObject.tag == ("Player" || "Crate")){
	target.transform.position.y = startHeight;
}
}

Help?

Scratch that, stupid error on my part.

New code:

var target : Transform;
var raisedHeight = 5.5;
private var startHeight : float;

function Start () {
startHeight = target.transform.position.y;
}

function OnCollisionEnter (other : Collision) {
if(other.gameObject.tag == ("Player" || "Crate")){
	target.transform.position.y = raisedHeight;
}
}

function OnCollisionExit (other : Collision) {
if(other.gameObject.tag == ("Player" || "Crate")){
	target.transform.position.y = startHeight;
}
}

No errors, but it still does nothing.

Your setup is still not clear, but i also think that the questions asked by others arent clear
the create needs a rigidbody (with gravity), and not a trigger
then this ‘button’ which has the posted script must not be rigidbody, but is a trigger.

lastly it seems you still didnt read the docs:

function OnTriggerEnter (other : Collider) {
you have
function OnCollisionEnter (other : Collision) {