Hi, I recently implemented art work into my game and made a new camera but after I attached the camera script an error occurs when I try to use my activate my raycast. Anyway, here is the code…
public class MainCameraScript : MonoBehaviour
{
private bool turret = false;
private bool trap = false;
public GameObject turret1;
public GameObject turret2;
public GameObject turret3;
public GameObject turret4;
public GameObject turret5;
public GameObject turret6;
public GameObject trap1;
public GameObject trap2;
public GameObject shotRange1;
private Texture dragTexture;
//private Texture trapTexture;
// Use this for initialization
void Start()
{
dragTexture=null;
//trapTexture=null;
}
void OnGUI()
{
if (dragTexture!=null)
{
GUI.DrawTexture( new Rect(Input.mousePosition.x,(Screen.height-Input.mousePosition.y),50,50),dragTexture);
}
/*if (trapTexture!=null)
{
GUI.DrawTexture( new Rect(Input.mousePosition.x,(Screen.height-Input.mousePosition.y),50,50),dragTexture);
}*/
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); //*ERROR OCCURS HERE*
RaycastHit hit = new RaycastHit();
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.gameObject.name == "TurretChoice")
{
turret = true;
dragTexture=hit.collider.gameObject.renderer.material.mainTexture;
}
if (hit.collider.gameObject.name== "TrapChoice")
{
trap = true;
dragTexture=hit.collider.gameObject.renderer.material.mainTexture;
}
/*if (Input.GetMouseButtonUp(0))
{
turret = false;
trap = false;
dragTexture=null;
}*/
}
}
if (trap == true)
{
if (Input.GetMouseButtonUp(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit = new RaycastHit();
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.gameObject.name == "TrapBase1")
{
trap1.active = true;
dragTexture=null;
}
if (hit.collider.gameObject.name == "TrapBase2")
{
trap2.active = true;
dragTexture=null;
}
dragTexture=null;
}
}
}
if (turret == true)
{
if (Input.GetMouseButtonUp(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit = new RaycastHit();
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.gameObject.name == "TurretBase1")
{
turret1.active = true;
shotRange1.active = true;
dragTexture=null;
}
if (hit.collider.gameObject.name == "TurretBase2")
{
turret2.active = true;
}
if (hit.collider.gameObject.name == "TurretBase3")
{
turret3.active = true;
}
if (hit.collider.gameObject.name == "TurretBase4")
{
turret4.active = true;
}
if (hit.collider.gameObject.name == "TurretBase5")
{
turret5.active = true;
}
if (hit.collider.gameObject.name == "TurretBase6")
{
turret6.active = true;
}
dragTexture=null;
}
}
}
if (Input.GetMouseButtonUp(0))
{
trap=false;
turret=false;
}
}
}
I have marked with comments where it tells me the error is occuring after receiving the error message " Null reference excpetion: Object reference not set to an instance of an object".
I can’t understand why this is happening, it worked fine before!!