Destroying objects, clearing an array
Hello,
I'm working on a very simple game. It displays 15 buttons and one of them is covered with a different sprite. It works fine for one round. But on the next round, i want to clear the array containing the 15 buttons to set a new one. And it s not working, i'm getting 15 new buttons on each round ( so it makes 30 buttons on the second round, 45 on the third...). I guess that my destroy function doesn't look for the good gameobject ( it's located in the "reussi" method) Thanks for your help !
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class InitBouttons : MonoBehaviour {
public Button prefabBoutton;
public Button[] listeBouttons;
public Transform monpere;
public Sprite recouvrement;
public GameObject pannelReussi;
public GameObject pannelTemps;
Text montexte;
int tirageAuSort;
int score = 0;
float timeMax= 15f;
float timeRemaining ;
bool reussite= false;
void Start ()
{
montexte= pannelTemps.GetComponent<Text>();
timeRemaining=timeMax;
initialisation();
}
void Update ()
{
if (!reussite) {
timeRemaining -= Time.deltaTime; // on soustrait au temps restant en secondes
UpdateTimeRemainingDisplay ();
}
if (timeRemaining <= 0f)
{
Debug.Log ("gameover");
}
if ( reussite && Input.GetMouseButton (0) )
initialisation();
//if (EventSystem.current.currentSelectedGameObject.name != null)
//Debug.Log (EventSystem.current.currentSelectedGameObject.name);
//if ( EventSystem.current.currentSelectedGameObject.name == listeBouttons[tirageAuSort].name) Debug.Log ("bravo!");
}
void initialisation ()
{
pannelReussi.SetActive (false);
reussite = false;
listeBouttons = new Button[15];
for (int i = 0; i < 15; i++)
{
listeBouttons[i] = Instantiate (prefabBoutton);
listeBouttons [i].transform.SetParent (monpere, false);
listeBouttons [i].name = "" + i;
Debug.Log (listeBouttons [i].name = "" + i);
}
tirageAuSort = Random.Range (0, 15);
//listeBouttons[tirageAuSort].GetComponent<Image>().color = Color.black; si on veut changer la couleur
listeBouttons [tirageAuSort].GetComponent<Image> ().sprite = recouvrement; // on change l'image
listeBouttons [tirageAuSort].onClick.AddListener (() => Reussi ());
}
bool Reussi ()
{
pannelReussi.SetActive(true);
score ++;
reussite = true;
timeMax --;
timeRemaining = timeMax;
for (int i = 0; i < 15; i++) // the loop to destroy clear the array by destroying the button
{
Debug.Log (" J'efface le bouton " + listeBouttons[i].name);
DestroyImmediate (listeBouttons [i]);
}
return reussite;
}
void UpdateTimeRemainingDisplay()
{
montexte.text = "Temps restant: " + Mathf.Round (timeRemaining).ToString (); // on affiche le tempts restant, Mathf.round arrondit
}
}
Answer by tieum67 · Feb 21, 2017 at 07:55 PM
well, i found a way to solve it .
i replace : DestroyImmediate (listeBouttons [i]); by Destroy(listeBouttons[i].gameObject);
Your answer

Follow this Question
Related Questions
Destroying 2D clones on touch 0 Answers
destroy particle system on collision 1 Answer
Component Image disabled after a spawn 1 Answer
destroy prefab over time 0 Answers
How to destroy the most ancient clone? 0 Answers