Search Unity

Cannot find Player gameobject with "FindWithTag" function

Discussion in '2D' started by RafaelRiva, Jun 2, 2019.

  1. RafaelRiva

    RafaelRiva

    Joined:
    Sep 14, 2018
    Posts:
    6
    Im trying to create a 2D runner where obstacles are created on the right and them they go to the left of the screen.

    One of the obstacles is a Pilar that you need to attack to remove.
    When a Pilar is spawned I try to get the player via FindwithTag function.


    PILAR FULL CODE:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using DarkTonic.PoolBoss;
    5.  
    6. public class ObstaclePilar : MonoBehaviour
    7. {
    8.     public static GameController GameManager;
    9.     public static GameObject game;
    10.     public static GameObject Player;
    11.     Animator anim;
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         anim.GetComponent<Animator>();
    17.         game = GameObject.FindWithTag("GameController");
    18.         GameManager = game.gameObject.GetComponent<GameController>();
    19.         Player = GameObject.FindWithTag("Player");
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.        
    26.     }
    27.  
    28.     public void DestroyThis()
    29.     {
    30.         PoolBoss.Despawn(transform);
    31.     }
    32.    
    33.     private void OnTriggerEnter2D(Collider2D other)
    34.     {
    35.         if (other.gameObject.CompareTag("Player"))
    36.         {
    37.             if (other = Player.GetComponent<CircleCollider2D>())
    38.             {
    39.                 anim.SetBool("Destroy", true);
    40.             }
    41.  
    42.             if (other = Player.GetComponent<CapsuleCollider2D>())
    43.             {
    44.                 GameManager.PlayerHit();
    45.             }  
    46.         }
    47.     }
    48. }
    But Im getting a Null Reference and I don't know why, since I already have use the same function to find the game manager and it worked fine.

    After that Im trying to check which collider the Pilar had encontered, the body of the player (Capsule) or the attack collider (Circle).



    Unity 2019.1.4f1 Personal - Prototipo.unity - AndroidRush_m2 - Android DX11.jpg Unity 2019.1.4f1 Personal - Prototipo.unity - AndroidRush_m2 - Android DX11_2.jpg
     
  2. orxyandcrake

    orxyandcrake

    Joined:
    Feb 15, 2019
    Posts:
    10
    Start by debugging the tag of "other" on collision
    Code (CSharp):
    1. Debug.Log(other.Name) //Check the name of the game object
    2. Debug.Log(other.Tag) //Check the Tag of the game object
    Also you have the Layer set to Default, is the object you are colliding with set to Default Layer aswell? You want them to be in the same Layer.
     
  3. broersma

    broersma

    Joined:
    Apr 30, 2015
    Posts:
    11
    Did you mean to do:
    Code (CSharp):
    1. anim = GetComponent<Animator>();
    Instead of:
    Code (CSharp):
    1. anim.GetComponent<Animator>();
     
  4. broersma

    broersma

    Joined:
    Apr 30, 2015
    Posts:
    11
    You probably want a comparison operator ('==') in line 37:

    Code (CSharp):
    1. if (other == Player.GetComponent<CircleCollider2D>())
    Instead of assignment ('='):

    Code (CSharp):
    1. if (other = Player.GetComponent<CircleCollider2D>())
    This doesn't directly explain the second NullReferenceException, I'd expect it to complain about not being able to implicitly convert to type 'bool'.