What I am trying to do is. When the raycast hits an object, take that objects and see if that game object is also in the array of game objects that was created. I have defined all items in the game object array. Here is a snippet of code. I cannot quite figure out why it works but at the same time also throws a reference exception.
public class GameManager : MonoBehaviour
{
public GameObject[] tiles;
private string temp;
private string nameToCheck;
void Awake()
{
SetupGame();
}
void Start ()
{
}
void Update ()
{
CheckRayCast();
CheckRayCastTouch();
}
void CheckRayCast()
{
if(Input.GetMouseButtonDown(0))
{
Vector3 mousePos = Input.mousePosition;
Vector3 screenPos = Camera.main.ScreenToWorldPoint(mousePos);
RaycastHit2D hit = Physics2D.Raycast(screenPos, Vector2.zero);
if(hit)
{
temp = hit.collider.name;
nameToCheck = temp;
foreach(GameObject x in tiles)
{
if(nameToCheck == x.name)
{
Debug.Log(hit.collider.gameObject.name);
}
}
}
Here it will give me the name of the object hit in debug but it also throws a reference exception. Not sure whats going on here.