[英]How i can disable multiple components Unity 3d c#
我如何禁用游戲對象的多個組件?
public void SaveComponents(GameObject obj, out List<Package> lista)
{
print("SAVE");
//Pobieramy wszystkie komponenty z obiektu
Component[] components = obj.GetComponents(typeof(Component));
}
那個例子
components[1].enable = false;
對我不起作用,“啟用”它不可用:/
這是因為Component
沒有名為enable
字段。 該Behaviour
在MonoBehaviour
的父類的Behaviour
中可用。
因此,如果要禁用所有組件, GameObject
需要禁用父GameObject
但是如果要禁用自己的數據類型的組件,請使用:
public void SaveComponents(GameObject obj, out List<Package> lista)
{
print("SAVE");
//Pobieramy wszystkie komponenty z obiektu
foreach(Behaviour behaviour in obj.GetComponents<Behaviour>()){
behaviour.enabled = false;
}
}
“ Component []組件= obj.GetComponents(typeof(Component)); <---這是我的數組,我想禁用數組中選定的元素” :
// list of all components without enable field in it
Component[] components = obj.GetComponents(typeof(Component));
int indexOfComponentToChange = 1; // index of the component to disable
Behaviour castedToBehaviour = components[indexOfComponentToChange] as Behaviour;
if ( castedToBehaviour != null )
{
castedToBehaviour.enabled = false;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.