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)
I had a hunch, this is what was happening, thanks
– NINJ4ZSweet, if that fixed your problem could you please mark it as the correct answer?
– darthbatorThis post needs to be more accessible, its the only thing that has worked for me.
– j_thomas362I have been searching for 3 hours trying different methods and this is the one that worked for me. Tho I might have been searching for the wrong stuff but I saw similar codes to this but never works. THANK YOU SO MUCH FOR THIS ANSWER! :D
– GrafSpee