I have the following issue in my project:
I just want my character to shoot at the enemy when they are in the same X or Y in grid position.
I've done this but I get a null reference message:
public void TeslaShooting()//Tesla's shooting method
{
for (int i = 0; i < Personagens.Length; i++)
{
if (Personagens[i].tag == "Tesla" && Input.GetMouseButtonDown(1) && Personagens[i].GetComponent<Jogador>() &&
Personagens[i].GetComponent<Jogador>().PodeAtacar == false && Personagens[i].GetComponent<Jogador>().isPlayer)
{
//raycast checks and returns an object, if it's a tile, the unit shoots
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
//checks if the tile is available based on the line and column of the unit
Tile tileAux = hit.transform.gameObject.GetComponent<Tile>();
Jogador scriptJog = player.GetComponent<Jogador>();
if (tileAux.TilePostion.x == scriptJog.GridPosition.x || tileAux.TilePostion.y == scriptJog.GridPosition.y)
{
if (tileAux.TilePostion.x > scriptJog.GridPosition.x)
tileAux.TilePostion.x = 5;
else
tileAux.TilePostion.x = 0;
if (tileAux.TilePostion.y > scriptJog.GridPosition.y)
tileAux.TilePostion.y = 5;
else
tileAux.TilePostion.y = 0;
Debug.Log(tileAux.TilePostion.x);
Debug.Log(tileAux.TilePostion.y);
if (hit.transform.tag == "Espantalho" || hit.transform.tag == "Xama" || hit.transform.tag == "Sheriff" || hit.transform.tag == "Cruz"
|| hit.transform.tag == "Louco")
{
//instantiates the bullet
GameObject tiro = Instantiate(projetil, SpawBala.transform.position, SpawBala.transform.rotation);
Rigidbody BalaRigid = tiro.GetComponent<Rigidbody>();
tiro.transform.LookAt(hit.transform.position);// to rotate the projectile in the direction of the enemy
player.transform.LookAt(hit.transform.position);// same above
PlayerRotation = player.transform.eulerAngles.y;// it stores how many degrees the player's rotated in Y angle
BalaRigid.velocity = SpawBala.transform.forward * ProjetilVeloc * Time.deltaTime;
gameManager.PontoAcaop1--;
Girou = true;
Personagens[i].GetComponent<Jogador>().PodeAtacar = true;
interfaceManager.ActionPointDown();
StartCoroutine(RotateBack(1));
}
}
}
}
}
}
I get the message in this line:
if (tileAux.TilePostion.x == scriptJog.GridPosition.x || tileAux.TilePostion.y == scriptJog.GridPosition.y)
I don't know why or what I've done wrong.