- Home /
FindWithTag always Returning null
I'm trying to create a very simple AI by getting an enemy game object to find the player and follow it. Although the Player is tagged with player the function FindWithTag seems to always be returning null and I cannot figure out why. I have looked around for days without finding anyone with a similar problem to mine.
My enemy controller script uses the FindWithTag to find the player game Object:
#pragma strict
public var health = 10f;
public var damage = 5f;
public var moveSpeed = 5f;
public var testPlayer : GameObject;
/*private var stats : enemyStats;*/
private var currentHealth = health;
private var player : GameObject;
private var rb : Rigidbody2D;
function start(){
/*stats = new enemyStats(health, damage);*/
if(player == null){
player = GameObject.FindWithTag("Player");
}
rb = GetComponent(Rigidbody2D);
}
function OnCollisionEnter2D (collision : Collision2D){
//Check that the player is colliding with a powerup
if(collision.gameObject.tag == "Projectile"){
Destroy(collision.gameObject);
//currentHealth -= bulletDamage;
if(currentHealth <= 0){
Destroy(gameObject);
}
}
}
function Update(){
Debug.Log(player);
Debug.Log(testPlayer.tag);
/*transform.LookAt(player.transform);*/
/*rb.AddForce(transform.forward * moveSpeed, ForceMode2D.Impulse);*/
}
The Debug.Log statements write null and Player respectively and the collision with the projectile objects works fine and the tag is detected properly. Both the player and the enemy object are instantiated by a gamecontroller, the player right at the beginning and the enemy a few seconds after. My player game object is correctly tagged with the Player tag.
I can't seem to find out why the FindWithTag function is returning null.
Thank you for noticing my silly mistake, that totally fixed my issue.
Answer by Patrick2607 · May 15, 2017 at 08:04 PM
Let me post it as an answer for other people. Some are too lazy to check the comments, no offense :).
The start function should be written with a capital S. Functions are case-sensitive.
Comments are intended for comments - additional information, suggestions, or requests for clarification of the problem. Answers should be used (and only used) when you are suggesting a method to resolve the problem asked.
Which I just did. Doesn't take away the fact that it's true. I myself am found guilty of that sometimes too :).
Answer by Fritsl · May 14, 2017 at 08:20 PM
Now, I'm a C# man, so I'm not sure, but it appears to me that this is what's going on:
private var player : GameObject;
Now, that variable is not Null, is it?
if(player == null){ player = GameObject.FindWithTag("Player"); }
That probably never happens, does it?
Sorry if I'm wrong :)
Yeah I know that's not the issue because I've tried it without the if statement and I get the same result, also in js the variable is declared but it hasn't been instantiated so it is null, the if statement is to make sure that you aren't overwriting an existing object. Thanks for the reply though
Answer by EmersonShen · May 15, 2017 at 06:46 PM
I found the issue, as @Patrick2607 pointed out my start function is not capitalized. Thank you for the replies.