hi, I’m trying to switch the camera when I click on an object, and I’m using this script
using UnityEngine;
public class RaycastDemo : MonoBehaviour {
public Transform target1, target2;
public Camera camera1;
public Camera camera2;
void Start () {
camera1.enabled = true;
camera2.enabled = false;
}
void Update () {
if (Input.GetMouseButton(0)) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit,10)) {
if (hit.transform == target1) {
camera2.enabled = true;
camera1.enabled = false;
Debug.Log("Hit target 1");
} else if (hit.transform == target2) {
Debug.Log("Hit target 2");
}
} else {
Debug.Log("Hit nothing");
}
}
}
}
the problem is that when I click on the object I have this error:
NullReferenceException
UnityEngine.Camera.ScreenPointToRay (Vector3 position)
RaycastDemo.Update () (at Assets/4.VirtualTour/script/RaycastDemo.cs:12)
I’ve tried to switch the camera with getKey and it works perfectly, but I need to use it with touch screen… somebody can help me?