Hello,
I’m very new to this and I’m trying to write a javascript for a game object (which has the rigidbody component attached) to destroy another javascript that is attached to the same game object after it collides with another object (which has the “is Trigger” box checked in the box collider component).
This is the script that I’m using:
function OnTriggerEnter(other : Collider)
{
// Destroy the script
Destroy(gameObject.GetComponent(MovementControl));
//setting rigidbody gravity boolean to true
rigidbody.useGravity = true;
}
The object with the scripts attached (OnTriggerEnter and “MovementControl”) is being moved toward the other object (with “is Trigger” enabled) and when the objects enter the trigger, the moving object keeps the script and continues moving right through the other object.
On another note… When I uncheck the “is Trigger” enabler, and give it a go, the script is destroyed when the objects collide, which, for me, makes no sense at all.
If anyone could shed some light on this and tell me what I’m doing wrong, it would be much appreciated.
Thanks,
Your basically dealing with 2 objects… A trigger. and a collider to move into that field
This is one of the basic principles of Unity.
“All components have common elements.”
All components have a gameObject component, and a transform and so on. These match the scripts that are on them. You reference them from the object that you are working on.
function OnTriggerEnter(other : Collider){
Debug.Log(other.gameObject.GetComponent(MovementControl));
}
You can see where I reference the other collider’s gameObject, then get a component from that.
So to destroy it, it is much the same way:
Destroy(other.gameObject.GetComponent(MovementControl));
Thanks for the quick reply. I tried the code that you supplied, but it’s still doing the same thing. I’m not sure if I explained myself clearly, but the game object that this script is attached to, is the same game object that the “MovementControl” script is attached to.
I did try to attach your script to the other object, which slowed the (MovementControl) object down, but didn’t stop it.
That script is attached to the object that is not the rigidbody. (The rigidbody should not have a trigger on it)
You are correct, there is nothing to destroy a script if you just copied it over, and nothing to turn on gravity. You will ahve to supply those.
ok, so a script with OnTriggerEnter has to be attached to an object with “is Trigger” enabled?? Sorry for the newbie question
Ok,
Here is the modified script that I tried on the “is Trigger” object. I still haven’t had any luck…
function OnTriggerEnter(other : Collider)
{
// Destroy the script
Destroy(other.gameObject.GetComponent(MovementControl));
if (other.attachedRigidbody) {
//setting rigidbody gravity boolean to true
other.attachedRigidbody.useGravity = true;
}
}
So, this is what I want to have happen:
-“Cube A” has a rigidbody and a script called “MovementControl” attached so the player can move it.
-“Cube B” has the “is Trigger” enabled and the script above attached to it.
“Cube A” is supposed to collide with “Cube B” and after doing so, the “MovementControl” script attached to “Cube A” should be destroyed and it should come to a stop on top of “Cube B”. Instead of stopping, “Cube A” passes directly through “Cube B” and losses the “MovementControl” script when it collides with other objects that don’t have the “is Trigger” enabled. When I disable the “is Trigger” on “Cube B”, the “MovementControl” script is destroyed when the 2 cubes come in contact, which is strange to me, but I’m brand new to this stuff.
Thanks for all your help.
OOOPPS!! The script has been fine the whole time!! The problem is that I wanted “Cube A” to stop on top of “Cube B”, but instead, it enter into the collision, which is “Cube B”'s mesh I’m guessing and loses it’s MovementControl and Rigidbody gravity thereafter and lands on the object below. I thought the object below is what was stopping “Cube A”.
Looks like I will have to make an invisible object with “isTrigger” enabled just above “Cube B”, that way “Cube A” will lose the script and gain the gravity before it enters "Cube B"s mesh and hopefully it will stop… I’m sure it will!!