简体   繁体   English

当游戏对象与主角碰撞时,如何破坏整个预制件?

[英]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.

相关问题 当玩家与触发器发生碰撞时,如何启用游戏 object 的组件? - How do I enable a component of a game object when the player collides with a trigger? 如何拥有多个精灵的游戏 object 预制件? - How to have a game object prefab with mutiple sprites? 加载预制件时如何修复游戏冻结 - How to fix game freeze when loading prefab 如何在新游戏开始时使用“Don't Destroy On Load”销毁游戏 object - How to destroy a game object with “Don't Destroy On Load” when a new game starts 与物体碰撞时如何制作文本透明度? - How to Make Text Transparencies when Collides with object? 如果游戏对象与某些物体发生碰撞而被破坏时,会产生逻辑数量的逻辑问题吗? - Logic issue spawning a set number of game objects when game object is destroyed if collides with something? 当我的统一游戏开始时,我的角色与它没有接触的物体发生碰撞 - When my game in unity starts, my character collides with objects it isn't touching 如何让游戏对象将更改应用于脚本中的预制件 - how do i get a game object to apply changes to a prefab in script 如何将游戏对象从层次结构设置为预制件。 统一 - How to set a game object from hierarchy to a prefab. unity 销毁对象并在原始对象位置替换为预制件? - Destroy an object and replace with a prefab at original objects location?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM