- Home /
Question by
Domo23000 · Aug 21, 2014 at 01:03 AM ·
cameraerrorraycasttower-defense
RaycastError
I am using a raycast script to place a 2d object in 2d space, but it keeps coming up with 'Object reference not set to an instance of an object' When i use the same line of code in a 2d world with only 3d objects, it works fine.
void Update ()
{
mousePos = Input.mousePosition;
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay (mousePos); // it selects this line
if (Physics.Raycast (ray, out hit, 1000))
{
if(hit.transform.tag == "PlaceablePath")
{
for(int i = 0; i < towers.Count; i ++)
{
if (towers[i].selected == true && Input.GetButtonDown ("Place"))
{
towers[i].selected = false;
GameObject clone = Instantiate(towers[i].towerObject, hit.transform.position, Quaternion.identity) as GameObject;
}
}
}
}
}
Comment
Answer by KMKxJOEY1 · Aug 21, 2014 at 01:26 AM
check towers is nut null, that each tower has a towerObject that is not null, you could probably give more context to your error because it will refer to a specific line...
Your answer
Follow this Question
Related Questions
Raycast doesn't collide as it should 1 Answer
Make the terrain ignore Raycast if in between Camera and Player? 1 Answer
Raycast Object Selection 3 Answers
Help with Raycast on Render Texture 0 Answers