• 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 /
avatar image
0
Question by $$anonymous$$ · Oct 14, 2017 at 06:50 PM · pragma

my LIST is empty why ?

Hello,

On Start() debug.log say List count = 3, it's OK. But when i click on button i call method Remove(), and in this method i have debug.log say List count = 0, WHY ? this is the problem.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class NewBehaviourScript : MonoBehaviour {
 
     Voiture car;
     List<Voiture> mesVoitures = new List<Voiture>();
 
     public GameObject prefab;
     GameObject[] items = new GameObject[3];
 
     void Awake() {
         mesVoitures.Add(new Voiture("BMW", 10, 1400));
         mesVoitures.Add(new Voiture("AUDI", 9, 1500));
         mesVoitures.Add(new Voiture("VW", 7, 1000));
     }
 
     void Start () {
         GetListe();
 
         // HERE it's work, 3 items in my LIST
         Debug.Log("List count: " + mesVoitures.Count);
     }
 
     void GetListe() {
 
         int nbr = 0;
 
         foreach (Voiture item in mesVoitures) {
             
             Debug.Log(string.Format("Constructeur: {0} CV: {1} Kg: {2}", item.constructeur, item.puissance, item.poids));
 
             items[nbr] = Instantiate(prefab);
             items[nbr].transform.name = "Item" +nbr;
             items[nbr].transform.parent = GameObject.Find("Canvas/Panel").transform;
             GameObject.Find(items[nbr].name + "/Background/Description").GetComponent<Text>().text = string.Format("Constructeur: {0} CV: {1} Kg: {2}", item.constructeur, item.puissance, item.poids);
             nbr++;
         }
     }
 
     public void Remove(GameObject name) {
         string n = name.name.Substring(4);
         int id = int.Parse(n);
         Debug.Log("TEST: " + id);
 
         // Here problem, 0 items in my LIST
         Debug.Log("List count: " + mesVoitures.Count);
 
         //mesVoitures.RemoveAt(id);
         DestroyObject(name);
 
     }
 
 }
 
 class Voiture {
 
     public string constructeur;
     public int puissance;
     public int poids;
 
     public Voiture(string constructeur, int puissance, int poids) {
         this.constructeur = constructeur; 
         this.puissance = puissance;
         this.poids = poids;
     }
 }
Comment
Add comment · Show 2
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
avatar image fafase · Oct 14, 2017 at 07:02 PM 0
Share

Are you sure the Remove method is not called twice in a row?

I see you use the same log Debug.Log("List count: " + mesVoitures.Count); and it could be you have 3 prints.

But if you have cascade in console then you see only two prints.

avatar image $$anonymous$$ · Oct 14, 2017 at 07:23 PM 0
Share

Another exemple, if i try to do RemoveAt(0) in my method Remove() im out of range because my list is empty in this area.

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

69 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

Related Questions

What is the function of the #pragma commands 1 Answer

Lies! You don't need pragma.etc for lists! 1 Answer

ios cant access variables in array 1 Answer

pragma strict create List of Array of strings for class? 1 Answer

#pragma warning disable on a global scale from within 1 script only? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges