从脚本中删除组件 [英] Remove component from script

查看:83
本文介绍了从脚本中删除组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用脚本从Gameobject中删除组件?

Is there any way to remove component from Gameobject using script?

例如:

我通过脚本将FixedJoint添加到播放器,将对象连接到它(用于抓取),当我放下它时,我想删除FixedJoint(因为我不能只是禁用"关节).我该怎么办?

I add FixedJoint to player by script, connect object to it (for grabbing), and when I drop it I want to remove the FixedJoint (because, I can't just "disable" joint). How can I do it?

推荐答案

是的,您使用 Destroy 函数可从GameObject中销毁/删除组件.可以用来删除Component或GameObject.

Yes, you use the Destroy function to destroy/remove a component from a GameObject. It can be used to remove Component or GameObject.

添加组件:

gameObject.AddComponent<FixedJoint>();

删除组件:

FixedJoint fixedJoint = GetComponent<FixedJoint>();
Destroy(fixedJoint);

这篇关于从脚本中删除组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆