我摧毀一個對象在我的遊戲在該方法中:團結「類型的對象已被破壞」的錯誤
public override void triggerAction(GameObject cube)
{
base.triggerAction(cube);
if (cube.GetComponent<Cube>().type == type) {
DestroyObject(cube);
}
}
,我得到了一個 'The object of type 'Cube' has been destroyed but you are still trying to access it.'
錯誤指向這個方法(第一行) :
if (Physics.Raycast(gameObject.transform.position, rayDir, out hit, 1f) && (hit.collider.gameObject.layer == layerMask)) {
ActionObject obj = hit.collider.gameObject.GetComponent<ActionObject>();
obj.triggerAction(gameObject);
}
在'Update'循環中調用此方法。
我在電視上看到我們來測試一下,如果遊戲對象不爲空淨,但是當我測試了一下,我上了條件相同的錯誤:
if (gameObject != null) { ... }
謝謝您的回答!
編輯:這是我如何實例化我的目標=
public GameObject cube; // I put my prefab in the inspector
...
GameObject newCube = Instantiate(cube) as GameObject;
if(GetComponent()!= null){...}我不確定,但嘗試。 –
當我寫它時,光線投射條件不起作用。 – Karz
不工作?錯誤? –