Reputation: 49
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...
Upvotes: 0
Views: 379
Reputation: 959
You were trying to destroy the current item from the list you were enumerating through, but this is an input field which you can cannot destroy. Instead by referring to the gameobject it belongs to and destroying that, you remove the Input fields.
Upvotes: 2
Reputation: 49
I did this and it worked
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()
}
Upvotes: 0