Turn Off A Script During Collision
Hey, I'm really confused on how to disable a script of another object when the player is colliding with a game object. The script name is "FauxGravityAttractor" and the game object the player will be colliding with is named "victoryCube" Thanks in advance!
Answer by ShadoX · Apr 04, 2016 at 11:46 AM
Something like this should work, though it's not tested, so it might be a bit incorrect:
void OnCollisionEnter(Collision collision) {
collision.gameObject.GetComponent<FauxGravityAttractor>.enabled = false;
}
https://unity3d.com/learn/tutorials/modules/beginner/physics/on-collision-enter http://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html
And of course if you want to re-enable the script after the collision:
void OnCollisionExit(Collision collision) {
collision.gameObject.GetComponent<FauxGravityAttractor>.enabled = true;
}
http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.OnCollisionExit.html
Your answer
![](https://koobas.hobune.stream/wayback/20220612074138im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
CapsuleCollider check if grounded 1 Answer
OnTriggerEnter2D executed once 0 Answers
Rigidbody going through wall during pinch-to-zoom 0 Answers