销毁所有预制克隆的实例不起作用
问题描述:
我正在编写一个重启函数,我想删除属于面板的所有预制件的实例.此代码向父级添加了一个新的输入字段
I am writing a restart function where i want to delete the instance of all prefabs belonging to a panel. This code adds a new input field to the parent
private void ShowInputField(){
GameObject inputFieldGameObject = inputFieldObjectPool.GetObject();
//reset pool since it may have been disabled by the DeActivatePreviousInputFields function
InputField inputField = inputFieldGameObject.GetComponent<InputField>();
inputField.Select ();
inputField.ActivateInputField ();
inputField.interactable = true;
inputField.readOnly = false;
//make this the previous inputs
previousInputs = inputFieldGameObject;
//assign the prefab to its parent panel
inputFieldGameObject.transform.SetParent (inputFieldParent);
inputFieldGameObject.transform.localScale = Vector3.one;
Canvas.ForceUpdateCanvases ();
//make scroll bar always show the last added input field
GameObject.Find ("ScrollView").GetComponent<ScrollRect> ().horizontalNormalizedPosition = 1;
}
下图显示了重启前的样子
The image below shows how it looks like before the restart
销毁所有预制实例并再次添加一个实例的重启代码如下所示.
the code for the restart which destroys all instances of the prefab and adds one again is shown below.
void RestartGame(){
//gets all elements of the parent
InputField [] childElements = letterPanel.GetComponentsInChildren<InputField> ();
Debug.Log (childElements.Count());
foreach (var item in childElements) {
Destroy (item);
}
ShowInputField()
}
这应该会破坏预制件的所有实例,但它不仅使它们可编辑,如下图所示
this is supposed to destroy all instances of the prefab but it does not it only makes them editable with the image below showing it
我该如何解决这个问题?提前谢谢你...
How do I solve this problem? Thank you in advance...
答
我做到了,并且奏效了
void RestartGame(){
//gets all elements of the parent
InputField [] childElements = letterPanel.GetComponentsInChildren<InputField> ();
Debug.Log (childElements.Count());
foreach (vInputField item in childElements) {
if(item.name.Contains("InputField")){
Destroy (item.gameObject);
}
}
ShowInputField()
}