- Home /
Triggering objects to be Active
Hello All
Semantics are killing me
I'm colliding with an object to switch it ie. trigger it to not be active and another to be active
All objects w script have triggers checked / colliding Objects have colliders and rigidbody kinematic
It works But Only Once
public GameObject target2Go;
public GameObject target2Come;
//Problem only works once
void OnTriggerEnter(Collider other)
{
if (target2Go.active == false)
{
target2Go.SetActive(true);
Debug.Log("go2");
target2Come.SetActive(false);
Debug.Log("come2");
}
if (target2Go.active == true)
{
target2Go.SetActive(false);
Debug.Log("YO GO");
target2Come.SetActive(true);
Debug.Log("come");
}
}
switched to OnCollisionEnter also not working btw I am doing this in VR
public class collEnter : MonoBehaviour {
public GameObject target2Go;
public GameObject target2Come;
// Doest work except second set of debug.Logs trigger
void OnCollisionEnter(Collision other)
{
if (target2Go.active == true)
{
target2Go.SetActive(false);
Debug.Log ("go");
target2Come.SetActive(true);
Debug.Log ("come");
}
if (target2Go.active == false)
{
target2Go.SetActive(true);
Debug.Log ("go2");
target2Come.SetActive(false);
Debug.Log ("come2");
}
}
}
Thanks / ~be
Answer by W01Fi3 · Oct 09, 2018 at 06:10 PM
A couple things, instead of using gameObject.active, try to use gameObject.activeSelf, as gameObject.active is obsolete.
Use OnTriggerEnter, OnCollisionEnter will work if IsTrigger == false
Also, try adding an else if statement, rather than two if statements.
if (target2Go.activeSelf == false)
{
target2Go.SetActive(true);
Debug.Log("go2");
target2Come.SetActive(false);
Debug.Log("come2");
}
else if (target2Go.activeSelf == true)
{
target2Go.SetActive(false);
Debug.Log("YO GO");
target2Come.SetActive(true);
Debug.Log("come");
}
Either that, or change it to something more compact, like what I have here:
target2Go.SetActive(!target2Go.activeSelf);
target2Come.SetActive(!target2Come.activeSelf);
This will work, as long as one of the objects is disabled at the start.
Answer by Bentoon · Oct 09, 2018 at 08:49 PM
AWESOME !! Thank you for Both of those & the fast reply!!