2
« on: July 20, 2013, 08:07:17 pm »
After looking at posts on this issue, I have inserted the following code in a script attached to all sprites that I want to check for being touched.
var hit:RaycastHit;
var ray:Ray;
if (Input.GetMouseButtonDown (0)) {
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (collider.Raycast (ray, hit, 100.0)) Debug.Log (myIndex);
}
Code compiles OK, but when "ray = ..." line is executed, I get message "NullReferenceException UnityEngine.Camera.ScreenPointToRay(Vector 3 position)"
I have one camera (MainCamera). I have attached a box collider to the sprites.
Can anyone explain what I'm doing wrong. I'm new to Unity and Java, so sorry if this seems trivial.
Also, any comments on whether or not the code as a whole will achieve my object (to obtain the index of the sprite that was touched) would be appreciated.