ZiNG
ZiNG

Reputation: 89

Unity / C# - gameObject.name returning inconsistent names

Hey guys so im learning C#/Unity and am making a space invaders type game and have come into an issue I am not understanding.

I have a class which is spawning my enemies (based on how many position gameobject children are inside this "Enemy Spawner"). I didnt like the name (clone) next to each one and wanted more feedback in my logs as I am starting to build the weapons/projectiles/collisions etc.

The issue: I get inconsistent results when printing the gameObject.name -- the initial load prints as expected (Enemy 1, Enemy 2, Enemy 3 etc). I have a projectile collision that is sending the hit dmg to the enemy but that is where the inconsistent name comes up -- it shows all enemies there as being just "Enemy".
tl;dr
Prints have the expected gameObject.name returning in Enemy Spawner and Laser, but not Enemy Controller after we receive dmg.

Enemy Spawner:

public class EnemySpawner : MonoBehaviour {
    public GameObject enemyPrefab;

    // Use this for initialization
    void Start() {
        var i = 1;

        //spawn enemy prefab for each position inside Enemy Spawner
        foreach (Transform child in transform)
        {
            GameObject enemy = Instantiate(enemyPrefab, child.transform.position, Quaternion.identity) as GameObject;
            enemy.transform.parent = child;
            enemy.name = ("Enemy " + i);
            print (enemy.name);
            i++;
        }
    }

}


Laser:

public class Laser : MonoBehaviour {
    public float laserDmg = 5f;
    public EnemyController enemyController;

    public void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.name.StartsWith("Enemy"))
        {
            enemyController.ReceiveDmg(laserDmg);
            Destroy(gameObject);
        }
        else
        {
            print("laser touched a " + collision.gameObject.name);
        }
    }
}


Enemy Controller:

public class EnemyController : MonoBehaviour {
    public float enemyHP = 40f;
    public string myName;

    // Use this for initialization
    void Start () {
        myName = gameObject.name;
        print(myName);
        gameObject.name = myName;
    }
    public void ReceiveDmg (float amount)
    {
        print(gameObject.name + " Took " + amount + " DMG || Now has " + this.enemyHP + " Remaining" );
    }
}

edit* the start vars in enemy controller was me trying to get the receiveDmg function to print the proper name using these vars (that print correctly on start) -- tried a bunch of other stuff too and figured I would ask since I am likely just mistaken on something trivial.

Upvotes: 1

Views: 808

Answers (2)

I.B
I.B

Reputation: 2923

It's normal you're getting the same name you're always calling the same script attached on the same object here

enemyController.ReceiveDmg(laserDmg);

You should be calling the script attached on the gameobject it just hit

collision.gameObject.GetComponent<EnemyController>().ReceiveDmg(laserDmg);

Upvotes: 2

Nabren
Nabren

Reputation: 582

This is most likely because EnemyController reference in Laser is not being set correctly.

It is not guaranteed to be the same GameObject as collision.gameObject in OnTriggerEnter2D with the code given and might be pointing to a prefab.

Upvotes: 0

Related Questions