Issues With SetActive
I'm having issues with setting my game object to true. In my script when you hit left control, it sends a raycast from the center of the camera screen, decides whether or not the thing it hits is a specific game object, then is supposed to set two different objects active; one being an empty game object and one being a canvas for UI. The second part of the code that sets both objects in-active works, and both Debug.Log (s) work, so i know the raycast is hitting and it also knows whether it is hitting the right game object or not; but the two gameobjects don't get set active.
public class RaycastNotes : MonoBehaviour {
public Transform Check;
public GameObject Safe;
public Transform SafePad;
public Camera MainCamera;
// Update is called once per frame
void Update ()
{
if(Input.GetKeyDown(KeyCode.LeftControl) && Check.gameObject.activeInHierarchy == false){
Debug.Log ("hit");
RaycastHit hit;
var cameraCenter = MainCamera.ScreenToWorldPoint(new Vector3(Screen.width / 2f, Screen.height / 2f, MainCamera.nearClipPlane));
if (Physics.Raycast(cameraCenter, MainCamera.transform.forward, out hit, 1000000000))
{
var obj = hit.transform.gameObject;
if (obj.transform.gameObject == Safe.transform.gameObject){
SafePad.gameObject.SetActive(true);
Check.gameObject.SetActive(true);
Debug.Log ("Same");
}
}
}
if(Input.GetKeyDown(KeyCode.LeftControl) && Check.gameObject.activeInHierarchy == true){
Check.gameObject.SetActive (false);
SafePad.gameObject.SetActive (false);
}
} }
Your answer
Follow this Question
Related Questions
Raycasting + setActive() 1 Answer
Highlighting with a Raycast 1 Answer
Set Object as active if Raycast hits nothing 0 Answers
adding range onto 2d raycast 3 Answers
Line of Sight and Raycasting 0 Answers