0

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.

1 Answers1

0

You Raycast is probably not hitting a tile, it's getting a physics hit with a collider, but probably not a gameobject with a tile. You need to add an additional check to confirm the hit has a tile like:

Tile tileAux = hit.transform.gameObject.GetComponent<Tile>();
if (tileAux == null) {
   ...
}

You can also add a LayerMask to specify the layers to use in a Raycast. This will allow you to only register hits for Tiles. Something like:

void Start()
{
    tileMask = LayerMask.GetMask("Tile");
}

And later use the tileMask:

if (Physics.Raycast (ray, out hit, 100f, tileMask)) {
   var tile = hit.transform.gameObject.GetComponent<Tile> ();
   ...
}
Samuel Goldenbaum
  • 15,346
  • 13
  • 51
  • 87
  • Exactly man, it wasn't hitting a tile, thank you. It worked perfectly @Samuel G. –  Dec 08 '19 at 12:37