[英]Unity2D cloned gameobject doesnt detect the tags of other gameobjects
我正在嘗試制作一個會產生的射彈,當它擊中玩家時,他會被摧毀。 我不得不提到彈丸將通過“實例化”命令產生,使其成為“克隆的游戲對象”。 在腳本中我寫道,如果射彈擊中另一個帶有“玩家”標簽的游戲對象,它擊中的游戲對象將被摧毀,但在運行代碼並且射彈擊中玩家后,他沒有被摧毀。 我檢查了一下,標簽上確實寫着“玩家”。 我在代碼中加入了一個調試命令,並設法發現沒有檢測到標簽。 射彈生成器的腳本和射彈本身是分開的,所以我將只展示射彈腳本,因為它是有問題的腳本。 我不得不提的是,該腳本不會產生任何錯誤,並且除了我上面提到的事情之外,模擬運行良好。
public class Bulletboi : MonoBehaviour
{
public float speed;
private Transform player;
private Vector2 target;
public GameObject Elven;
void Start()
{
player = GameObject.FindGameObjectWithTag("player").transform;
target = new Vector2(player.position.x, player.position.y);
}
void Update()
{
transform.position = Vector2.MoveTowards(transform.position, target, speed * Time.deltaTime);
if(transform.position.x == target.x && transform.position.y == target.y)
{
DestroyProjectile();
}
}
void OnEnterTrigger2D(Collision2D other)
{
if (other.gameObject.tag.Equals("player"))
{
Debug.Log("bbbb");
DestroyProjectile();
Destroy(other.gameObject);
}
}
void DestroyProjectile()
{
Destroy(gameObject);
}
}
沒關系,我決定稍微更改一下腳本,然后將其放在播放器上並讓它檢測到射彈的標簽,現在它可以運行了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.