- Home /
Why won't OnCollisionEnter work here (C#)
I'm obviously an idjut & missing something very basic...
I have a Camera colliding with an invisible Plane to trigger a GUI event Plane has collider (even a rigid body though I doubt I need) My Camera also has a Box Collider on it just to make sure
Why won't my OnCollisionEnter script won't work?
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
public class triggerGUI : MonoBehaviour {
public GameObject toAppear;
public GameObject toDisappear;
void OnCollisionEnter (Collision camera ) {
Debug.Log ("Collide");
toDisappear.SetActive (true);
toAppear.SetActive (false);
}
}
Thanks Y'all
~be
Are the colliders on either the camera or the plane set to trigger? In that case you won't get a collision, and OnCollisionEnter won't fire.
What you want, though, is probably not a collision, but a trigger event. Which means that you want to make both of them triggers, and use OnTriggerEnter ins$$anonymous$$d.
Thanks Baste The Trigger is selected on both still won't work, Do I need to pass it anything?
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
public class triggerGUI : $$anonymous$$onoBehaviour {
public GameObject toAppear;
public GameObject toDisappear;
void OnTriggerEnter() {
Debug.Log ("Collide");
toDisappear.SetActive (true);
toAppear.SetActive (false);
}
}
void OnTriggerEnter(Collider camera)
doesn't seem to work either
Answer by Bentoon · Oct 05, 2014 at 12:12 AM
Thank you Baste it is working!! Here is the script:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
public class triggerGUI : MonoBehaviour {
public GameObject toAppear;
public GameObject toDisappear;
void OnTriggerEnter(Collider camera) {
Debug.Log ("Collide");
toDisappear.SetActive (false);
toAppear.SetActive (true);
}
}