- Home /
The question is answered, right answer was accepted
Activate Script not Working
I have this script attached to a spaceship:
var Whatever : GameObject;
function OnTriggerEnter (other : Collider) {
var scriptname : SeekSteer = Whatever.GetComponent("SeekSteer");
scriptname.active = true;
}
The space ship has a a box collider. The box collider has a Trigger. When I shoot the Collider with a bullet (that doesnt have a Trigger). Nothing happens. What am I doing wrong? I also tried attaching the script to another object, and it still doesnt work.
NOTE: The top 3 pictures show a few things about my object: http://unityimageprobs.wordpress.com/
Do you have a rigidbody attached to one of the objects?
Answer by fafase · Aug 08, 2012 at 05:59 PM
Try removing the "" in GetComponent:
var scriptname : SeekSteer = Whatever.GetComponent(SeekSteer);
You should by the way cache this command.
var scriptname :SeekSteer
function Start(){
scriptname = GameObject.Find("Whatever").GetComponent(SeekSteer);
}
function OnTriggerEnter (other : Collider) {
scriptname.active = true;
}
I wonder though if you can find a component that is not active.
That doesn't seem to be working either.... The Box Collider should be a trigger and my bullet shouldn't right? I attached the script to the Box Collider..
I would put that script on the trigger object. Are you sur ethe collision is detected already? Add this in the OnTriggerEnter
Debug.Log("You collide me real hard");
I noticed something really weird: So when I shoot the box collider at first, the bullets explode like they are supposed to, but after awhile they just start going straight through the collider... Is this supposed to mean something??
If the collision is detected, you should see You collide e real hard showing up in the console at the bottom of Unity.
OnTriggerEnter is supposed to detect collision but does not resolve it. It means it is like entering a zone without walls. So your bullet should go through. It is not easy to know without the rest of the scripts.
Answer by 3D Junkie · Aug 08, 2012 at 11:16 PM
the script is a component, not an object, so maybe use .enabled instead of .active?
That ended up being the problem, but i figured it out, Thanks!
Answer by TaintedNobodies · Aug 08, 2012 at 04:53 PM
Make sure your bullet that is being shot has a collider and a rigid body.
straight from the Unity Reference Manual:
For a Trigger to collide with a normal Collider, one of them must have a Rigidbody attached. For a detailed chart of different types of collisions, see the collision action matrix in the Advanced section below.
$$anonymous$$y bullet has a Collider and is a Rigidbody. The Box Collider inside the ship has the Trigger. That is correct, right?
Answer by Meldow · Aug 08, 2012 at 04:52 PM
try putting some Debug.Log("DetectedCollision"); so you are SURE that the collision occured. 90% of the times the code isnt doing what you want, its because u are considering things that arent happening.
still, little info to give a propper answear
Answer by TaintedNobodies · Aug 08, 2012 at 11:16 PM
it probably means the speed of the bullet is too fast. On the bullet object, make sure it has the rigid body connected to it. and under the rigid body options there should be a Collision Detection option where you can select. discrete, continuous, or continuous dynamic, i would select either continuous or continuous dynamic, those two options you would want to use for fast moving objects to detect collision.
at the bottom of the unity window you should see it say debug log with the You collide me real hard.
I would adjust your script to this. Add a get tag to make sure that the object coming in contact with the ship is a bullet. Which means make a bullet tag in unity and make sure that the bullet object has that tag. otherwise anything that comes into contact with it will trigger the script. that might be the thing that is messing with you, is if you have the script deactivated something like the ground or something flying or whatever with a collider might be coming into contact and turns on the script.
var Whatever : GameObject;
function OnTriggerEnter (other : Collider) { if (other.gameObject.tag == "bullet") { Debug.Log("You collide me real hard"); var scriptname : SeekSteer = Whatever.GetComponent(SeekSteer); scriptname.active = true; } }
second I would make a dummy script named SeekSteer with just this code
function Update() {
Debug.Log ("SeekSteer scripted turned on");
}
So that this is the script that is turned on. and if it is turned on in the debug log it will keep saying seek steer script turned on over and over and over so that you know you turned it on. Also I know you probably already did it, but make sure the script is unchecked that you want to turn on so it is intact deactivated
Follow this Question
Related Questions
Controller Collider Script 1 Answer
Detect if out of trigger 2 Answers
Action activates trigger. 1 Answer
Can't click gameobject when over another trigger? 1 Answer
How to get info from child triggers? 1 Answer