I’m trying to cast a raycast from wherever I touch on the screen. If that object tag is equal to “Player”, I want it to run my code but as soon as I hit anything other than Player, I receive an error.
Can someone shed some light on this?
void Cast()
{
for (int i = 0; i < Input.touchCount; ++i)
{
Vector2 test = Camera.main.ScreenToWorldPoint(Input.GetTouch(i).position);
if (Input.GetTouch(i).phase == TouchPhase.Stationary)
{
test = Camera.main.ScreenToWorldPoint(Input.GetTouch(i).position);
if(Physics2D.Raycast(test, (Input.GetTouch(i).position)).collider.tag == "Player")
{
Debug.Log("yup");
}
}
}
}
Here’s my error NullReferenceException: Object reference not set to an instance of an object
PlayerControl.Cast () (at assets/Scripts/PlayerControl.cs:170)
PlayerControl.FixedUpdate () (at assets/Scripts/PlayerControl.cs:153)