【发布时间】:2016-03-20 16:17:11
【问题描述】:
我在动态添加 tag 到 gameObejcts 时遇到问题,当我使用 Instantiate 和 enemy 添加敌人标签时:
GameObject temp = (GameObject)Instantiate(enemy, spwanPosition, spwanRotation);
temp.gameObject.tag = "Enemy";
我从Is it possible create a tag programmatically读到这个
但是当我尝试用特定的explosion 销毁这个对象时,没有带有Enemy 标签的对象:
void OnTriggerEnter(Collider other)
{
if (other.tag == "Boundary")
{
return;
}
if (other.tag == "Enemy")
{
Debug.Log("enemy"); # Never printed in console
Instantiate(enemyExplosion, other.transform.position, other.transform.rotation);
}
Instantiate(explosion, this.transform.position, this.transform.rotation);
Destroy(other.gameObject);
Destroy(this.gameObject);
如何将标签动态添加到gameObject 和Instantiate 多次?
编辑
这是暂停游戏中的实例化敌人对象的图片,它是如此连线,它有Enemy tag,但是为什么这不起作用?
问题是在运行时将Enemy 标记分配给对象,但是当我检查if(other.tag == "Enemy") 时,这不起作用!
【问题讨论】:
-
我很困惑。你说 Instantiated 对象没有标签,但你说它确实有标签...?
-
标签说敌人。名字说敌人(克隆)。层说默认。根据您的预期,哪个是错误的?
-
@fafase,我
Instantiate很多,都是他自己克隆的,我不知道是什么层 -
@Arman,改变你的问题不是是件好事。问题是关于重命名标签,我提供了如何正确更改的解决方案。你说它不起作用,但你继续用我的代码替换了原始代码。我让您上传的图片证明标签已更改现在已更改为敌人。当您的原始问题得到回答时,您接受它,然后如果您遇到不同的问题,请提出新问题。看起来您现在遇到了碰撞问题。不要改变你的问题!问问别人。如果你想展示你的新代码,
-
您将新代码放入 EDIT 但不要替换原来的代码。