Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by tieum67 · Feb 21, 2017 at 05:52 PM · destroy-clones

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
     }
 
 
 
 
 
 }
 

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

94 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges