[英]How to destroy the whole prefab when the game object collides with the main character?
What I'm trying to do is to make my monster disappear when my main char collides with it. 我要做的是让我的主要角色与我的怪物相撞时使其消失。 So, I attached this script to my monster, but I can't get it to work. 因此,我将此脚本附加到了我的怪物上,但无法使其正常工作。 I succeeded in destroying monster's RigidBody component but I can't seem to destroy the whole thing. 我成功销毁了怪物的RigidBody组件,但似乎无法销毁整个东西。
using UnityEngine;
using System.Collections;
public class Dying: MonoBehaviour {
private Rigidbody rbody;
public GameObject prefab;
void Start () {
rbody = GetComponent<Rigidbody>();
GameObject obj = Instantiate(prefab);
}
void Update () {
}
void OnCollisionEnter(Collision col)
{
print(col.collider.name);
if(col.collider.name =="unitychan")
{
Destroy(prefab.gameObject);
}
}
}
What might be the problem is that you are trying to destroy either the object running the script, or some other random object. 可能的问题是您试图破坏运行脚本的对象或其他随机对象。 You typically never destroy the object running the script. 通常,您永远不会破坏运行脚本的对象。 Also, col contains the object that was collided with. 另外,col包含与之碰撞的对象。 so you can just do this: 所以你可以这样做:
void OnCollisionEnter(Collision col)
{
if (!col.collider.name == "unitychan") return;
Destroy(col.gameObject);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.