hi,
total noob here
I’m trying to make a simple camera switch with sprite buttons. i try to detect the buttons from maincam with 2draycast. on mouse down jump to camera().
and it works.
i also have a UI canvas with one button: on click application.loadlevel(“blabla”);
when i press the UI Button the scene loads as i want.
but after loading the scene there is this error:
“NullReferenceException: Object reference not set to an instance of an object CameraSwitch.Update () (at Assets/Scripts/CameraSwitch.js:17)”
when i double click it, it jumps to the raycast line (script attached here)
i try to understand why. what is wrong?
sry for the bad english
iam very happy about your help
thank you
var VH: int = 0;
var Page9: GameObject;
function Start ()
{
VH = 1;
}
function Update ()
{
if (Input.GetMouseButtonDown (0))
{
var hit: RaycastHit2D = Physics2D.Raycast(Vector2(camera.main.ScreenToWorldPoint(Input.mousePosition).x,camera.main.ScreenToWorldPoint(Input.mousePosition).y), Vector2.zero, 0);
if (hit.collider.tag == "ButtonLeihservice")
{
VH = 6;
camSwap(6);
}
if (hit.collider.tag == "ButtonVR")
{
VH = 8;
camSwap(8);
}
if (hit.collider.tag == "ButtonAR")
{
VH = 11;
camSwap(11);
}
}
if(Input.GetKeyDown(KeyCode.DownArrow))
VH = VH +1;
if(Input.GetKeyDown(KeyCode.UpArrow))
VH = VH -1;
if(VH<1)VH=56;
if(VH>56)VH=1;
if (VH == 1)camSwap(1);
if (VH == 2)camSwap(2);
if (VH == 3)camSwap(3);
if (VH == 4)camSwap(4);
if (VH == 5)camSwap(5);
if (VH == 6)camSwap(6);
if (VH == 7)camSwap(7);
if (VH == 8)camSwap(8);
if (VH == 9)camSwap(9);
if (VH == 9) Page9.SetActive(true);
else Page9.SetActive(false);
if (VH == 10)camSwap(10);
if (VH == 11)camSwap(11);
}
function camSwap(currentCam : int)
{
var cameras = GameObject.FindGameObjectsWithTag("MainCamera");
for (var cams : GameObject in cameras)
{
cams.GetComponent(Camera).enabled = false;
}
var oneToUse : String = "Camera"+currentCam;
gameObject.Find(oneToUse).GetComponent(Camera).enabled = true;
}