- Home /
Set Gameobject active using if statement and collider
Hi i'm trying to set a game object active in my Unity 3D project (with oculus sdk).Hopefully this would be set active when the player collides with "triggerCube" (specifically want to be able to pick up "triggerCube" and then ViewSight gameobject is set active. When player lets go, ViewSight gameobject is set to false.
I've tried turning off ViewSight gameobject in the inspector but still having it in the hierarchy. It didn't work so I made a prefab of it and deleted it from the hierarchy (and pulled it in to the script in the inspector for the triggerCube) and it didn't work that way either. Not sure what I'm doing wrong.
Thanks for your help.
This is the script I have and its attached to the triggerCube:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ViewFinderStateTrigger : MonoBehaviour {
public GameObject ViewSight;
public void OnTriggerEnter(Collider col)
{
if (col.gameObject.name == "triggerCube")
{
ViewSight.SetActive(true);
Debug.Log("Active");
}
else
{
ViewSight.SetActive(false);
Debug.Log("Not Active");
}
}
}
Answer by gamesmarlac · Nov 06, 2018 at 01:13 PM
I think that could work.
public GameObject ViewSight;
public void OnTriggerEnter(Collider col)
{
if (col.gameObject.name == "triggerCube")
{
ViewSight.SetActive(true);
Debug.Log("Active");
}
else
{
ViewSight.SetActive(false);
Debug.Log("Not Active");
}
}
public void OnTriggerExit (Collider other)
{
if (other.gameObject.name == "triggerCube")
{
ViewSight.SetActive(false);
Debug.Log("Not Active");
}
}