所以在我的遊戲中,有一支噴射子彈的槍,並且我試圖在與子彈相撞時使gameObject摧毀。子彈是基於一個gameObject(膠囊)。到目前爲止,我已經試過這兩個腳本:對象沒有被子彈射擊時被破壞 - UNITY3D C#
using UnityEngine;
using System.Collections;
public class whenshot : MonoBehaviour {
void OnCollisionEnter(Collision col)
{
if (col.gameObject.name == "Bullet")
{
Destroy(col.gameObject);
}
}
}
和:
using UnityEngine;
using System.Collections;
public class whenshot : MonoBehaviour {
void OnCollisionEnter(Collision col)
{
if (col.gameObject.name == "Bullet")
{
Destroy(this); //the difference between the two is that I changed "col.gameObject" to "this"
}
}
}
我拍攝的對象,但它沒有消失/自我毀滅。我怎樣才能解決這個問題?
看來您的原始代碼正在銷燬子彈,而不是它擊中的對象。 –
將以下代碼行添加到函數的頂部,並告訴我們當您再次嘗試時在日誌中顯示的內容:Debug.Log(「name:」+ col.gameObject.name);'Debug'。 Log(「matches:」+(col.gameObject.name ==「Bullet」)。ToString());',如果你沒有得到任何日誌消息也告訴我們。 –
@SCOTTChamberlain如果日誌在控制檯下,那麼我沒有得到任何日誌消息。 –