Hi, I would like to know if it’s possible to rather than using a Game Object’s tag;
c.gameObject.tag == "Enemy"
instead use a Game Object’s actual name like so:
c.gameObject.name == "EnemyType_foo"
I would use this so that instead of having to create tags for all game objects, I’d just use the game object name and leave the tags for tagging game objects of a particular type (like Enemy, or Ally for example).
Unfortunately, while I am able to call the “name”, this does not work (in my case, collisions are not detected when game objects are referred to by name).
My question is; is there another way to achieve this? Is the “.name” referring to a different name rather than the game object’s itself?
Thank you!
Could you show us the code that didn't work?
– vexegameObject.namegives you back the object's name that you see in your your hierarchy. Tryprint(c.gameObject.name);see what you get.Just inside your collision code put this: Debug.Log(collision.collider.name); 'collision' is whatever you've named the parameter in OnCollisionEnter(). Names like tags must match exactly including case.
– robertbuvoid OnTriggerEnter(Collider c){ if (c.gameObject.name == "Bacteria"){ Destroy(c.gameObject); Destroy(gameObject); PointScript.allocatePoints(10); } } Unfortunately the collision between the cell (gameObject) and the "bacteria" (c.gameObject) is not being detected. But there are no compilation errors of any sort.
– jamesb1Just inside the OnTriggerEnter(), put: Debug.Log(c.name); This way you can see the name of the game object being hit.
– robertbuI did exactly what you said and I found that the name is actually "Bacteria(Clone)" since "Bacteria" is a prefab being instantiated continuously throughout the game! I guess it was just a case of making sure you're using the correct name. Thank you!
– jamesb1