2016-11-24 93 views
0

我的屏幕上有15個按鈕。 在onclick事件,我取的按鈕和隱藏使用使用c顯示隱藏按鈕#

EventSystem.current.currentSelectedGameObject.SetActive(false); 

最後我要再次顯示按鈕,按鈕,所以我用

for (int i = 0; i < 15; i++) 
{ 
    tag1 = "Button" + (i + 1); 
    GameObject.FindGameObjectWithTag(tag1).SetActive(true); 
    Debug.Log("done"); 
} 

環路是給錯誤,因爲它是無法找到已被隱藏的對象或設置爲(false) 從其他參考(unity forum)我看到相同的解決方案,但我不知道爲什麼它不適用於我的情況。

+0

會更容易得到這個按鈕在腳本的引用,並只需要調用'mebutton.SetActive(真)' –

+0

按鈕不包含SETACTIVE財產@ m.rogalski – phpdroid

+0

@JamesHughes我試圖重新啓動我知道的所有Button11被禁用之前,它打印「完成」10次只有問題不能「找到已被隱藏的對象」 – phpdroid

回答

0

由於您想通過標籤查找對象,並且無法使用方法FindGameObjectWithTag找到隱藏的元素。您可以使用此方法:

var allObjects = GameObject.FindObjectsOfType(typeof(GameObject)) as GameObject[]; // this will grab all GameObjects from the current scene! 
foreach(GameObejct obj in allObjects) { 
    if(new Regex(@"^Button(?'number'\d{1,2})").IsMatch(obj.Tag)) { 
     obj.SetActive(true); 
     Debug.Log("done"); 
    } 
} 
+0

InvalidCastException :無法從源類型轉換爲目標類型。 here >>> foreach(GameObejct obj in allObjects) – phpdroid

+0

[FindObjectsOfType](https://docs.unity3d.com/ScriptReference/Object.FindObjectsOfType.html)並查看已更新的答案。 –

+0

已完成195次完成記錄,但看不見的按鈕沒有出現 – phpdroid