- Home /
Unwanted occurence of collision event
Hi,
The picture above demonstrates what I want to achieve. Let me explain what this is all about and which part does not work as expected.
A, B are two GameObjects that are moving towards the direction of the arrows. Block1, Block2 are also GameObjects, stationary, both having rigidbody and a box collider.
Both A and B have a script attached on them that detects collision. Once collision occurs (OnTriggerEnter()), the object is destroyed.
Upon initialization, the script is disabled for A, because I do not want it to collide with Block1 (this is a temporary solution until I find what's happening).
An IEnumerator moves A and B towards the direction they're supposed to go. Now, although A's script has been disabled, it sometimes collides with Block1, getting itself destroyed. Other times it will go on as intended. If A still had the script enabled, this would make perfect sense. Testing the inspector during runtime, the script is indeed disabled.
Block1, Block2 have no script attached to determine what happens upon collision.
Any ideas on why this weird thing's happening?
public class CollisionEffects : $$anonymous$$onoBehaviour {
Vector3 initPosition;
void OnTriggerEnter(Collider col) {
if (col.gameObject.name == "Shield") {
gameObject.transform.position = initPosition;
string whichName = gameObject.name;
string newName = whichName.Substring(5, whichName.Length - 12);
Transform whichChild = col.gameObject.transform.FindChild (newName+"Particles");
whichChild.gameObject.SetActive (true);
gameObject.SetActive (false);
}
}
// Use this for initialization
void Start () {
initPosition = transform.position;
}
}
Answer by equus_ligneus · Apr 12, 2015 at 01:52 PM
Whether a script is enabled or disabled only affects the OnGUI(), Update(), FixedUpdate() and LateUpdate() methods (correct me if I forgot one). Every other callback is executed. You can use a bool to check if your GameObjects should be destroyed or not.
Care to explain why this happens? The script is basically a game object's component. If I set it to active, it will run normally. If it is disabled, it shouldn't. Right?
Disabling a script does not remove it from execution. As matey says, Collision routines, custom functions etc will all work even if the script is disabled.
bool check or name/tag check in your collision routine to selectively destroy.
At Pharaoh_: This is not a bug but a design choice. Enabling or disabling a component affects whether callbacks that are called every frame are executed. Other callbacks that are tied to conditions (Awake() to creation, OnTriggerEnter() to entering a trigger) are called so that a script can respond to them. At least that's my interpretation.
It's the same as destroying GameObjects:
Destroy(unityObjectYouWantToDestroy);
If a script wants to destroy itself it has to call
Destroy(this);
To fully answer your question: 'this' refers to the instance of a class you are currently in. If you call Destroy(this), only the instance on which Destroy(this) is called will be destroyed.
Your answer
Follow this Question
Related Questions
Trigger Spawning? 1 Answer
OnTriggerEnter and GUIText problem 0 Answers
Destroy object 1 if player walks into object 2 1 Answer
Colliding two GameObjects 1 Answer
Problem with OnTriggerExit 1 Answer