Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by bustedkrutch · Aug 30, 2013 at 03:51 PM · listsclasses

Trouble with Add-ing and Retrieving from a List of Custom Class

Hi Folks,

I would like to love Lists, really, and I'm sure there is lots to love, however...

I'm attempting to use a List of Custom Class and when I retrieve from any of the items in the List, they are ALL of the last item I .Add-ed to the List. Crazy yeah!

Here is the Class:

 public class Transport{
 
     var position            : Transform;
     var confirmation        : int;
     var    up                    : boolean;
     var    down                : boolean;
     var attempts            : int;
     
     public function Transport(){
         this.position        = null;
         this.confirmation    = 0;
         this.up                = false;
         this.down            = false;
         this.attempts        = 0;
     }
 }

Here are the declaration of two Lists one of the Custom Class "Transport" and a test List of int and a temp variable to .Add to the List of Transport: [outside any function]

 var transport_list            : List.<Transport>;        // List of nergs to transport up
 var xport                    : Transport;
 var carp_list                : List.<int>;            // List of carp


Here is the instantiation (not sure if that is quite the right term there - please correct if there is a more accurate one - thanks :) [inside Start]

     transport_list            = new List.<Transport>();
     xport                    = new Transport();
     carp_list                = new List.<int>();

Here is where I .Add to the Lists:

         xport.confirmation     = confirmation;
         xport.position        = loc;
         transport_list.Add(xport);
         carp_list.Add(confirmation);

Here is where I retrieve from the Lists (I included both variable and hardcoded indices for troubleshooting):

 function show_transport_list(){
     var i : int;
     for(i=0; i<transport_list.Count; i++){
         
         switch(i){
         
             case (0):
                 dl("mothership : transport_list[i].position.position = 'hardcoded' " + transport_list.Count +"  /  0  /  " + transport_list[0].position.position + " / " + transport_list[0].confirmation);
                 dl("mothership : transport_list[i].position.position = 'variable ' " + transport_list.Count +"  /  "+ i +"  /  " + transport_list[i].position.position + " / " + transport_list[i].confirmation);
                 break;
 
                 // The other cases from case (1) to case (3) are here but it was getting too lengthy.  you will see the output of it though
 
             case (4):
                 dl("mothership : transport_list[i].position.position = 'hardcoded' " + transport_list.Count +"  /  4  /  " + transport_list[4].position.position + " / " + transport_list[4].confirmation);
                 dl("mothership : transport_list[i].position.position = 'variable ' " + transport_list.Count +"  /  "+ i +"  /  " + transport_list[i].position.position + " / " + transport_list[i].confirmation);
                 break;            
         }        
     }
     
     for(i=0; i< carp_list.Count; i++){
         
         switch(i){
         
             case (0):
                 dl("mothership : carp_list[i] = 'hardcoded' " + carp_list.Count +"  /  0  /  " + carp_list[0] + " / " + carp_list[0]);
                 dl("mothership : carp_list[i] = 'variable ' " + carp_list.Count +"  /  "+ i +"  /  " + carp_list[i] + " / " + carp_list[i]);
                 break;
 
                 // The other cases from case (1) to case (3) are here but it was getting too lengthy.  you will see the output of it though
 
             case (4):
                 dl("mothership : carp_list[i] = 'hardcoded' " + carp_list.Count +"  /  4  /  " + carp_list[4] + " / " + carp_list[4]);
                 dl("mothership : carp_list[i] = 'variable ' " + carp_list.Count +"  /  "+ i +"  /  " + carp_list[i] + " / " + carp_list[i]);
                 break;            
         }        
     }
 }

And what we've all been waiting for..here is the output of the retrieval:

 mothership : transport_list[i].position = 'hardcoded' 3  /  0  /  (232.0, 24.1, 235.0) / 1004
 mothership : transport_list[i].position = 'variable ' 3  /  0  /  (232.0, 24.1, 235.0) / 1004
 mothership : transport_list[i].position = 'hardcoded' 3  /  1  /  (232.0, 24.1, 235.0) / 1004
 mothership : transport_list[i].position = 'variable ' 3  /  1  /  (232.0, 24.1, 235.0) / 1004
 mothership : transport_list[i].position = 'hardcoded' 3  /  2  /  (232.0, 24.1, 235.0) / 1004
 mothership : transport_list[i].position = 'variable ' 3  /  2  /  (232.0, 24.1, 235.0) / 1004
 mothership : carp_list[i] = 'hardcoded' 3  /  0  /  1002 / 1002
 mothership : carp_list[i] = 'variable ' 3  /  0  /  1002 / 1002
 mothership : carp_list[i] = 'hardcoded' 3  /  1  /  1003 / 1003
 mothership : carp_list[i] = 'variable ' 3  /  1  /  1003 / 1003
 mothership : carp_list[i] = 'hardcoded' 3  /  2  /  1004 / 1004
 mothership : carp_list[i] = 'variable ' 3  /  2  /  1004 / 1004

The tell tale is that the last entry in both of the Lists is represented by the same value in the last entry in the List "carp_list" and in ALL entries of the List "transport_list".

What in the world am I doing incorrectly?

Thank you :)

Comment
Add comment · Show 7
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 perchik · Aug 30, 2013 at 04:00 PM 0
Share

I'm not entirely sure about the List. syntax over List but I'm going to write that off as syntactic sugar (although that might very well be the problem)

I want to see more of your add code. I think you're either adding the same thing to the list multiple times, or you're accessing the same item multiple times.

I'd add a string "name" to your transport class for debugging. When you create an instance of transport, give it a unique name, then tweak your output to say something like `Debug.Log("transport_List[" + i + " ] = " + transport_list[i].name);

That way, you could see if you're adding the same thing multiple times.

avatar image perchik · Aug 30, 2013 at 04:06 PM 0
Share

@Bustedkrutch does create a 'new' object:`xport = new Transport();`.. it looks like it should be right, but I don't know if that new object is created for each object, or just once

avatar image bustedkrutch · Aug 30, 2013 at 04:26 PM 0
Share

You guys/gals? are awesome!!!

I think I know how to solve it now, with your input. I'll be right back.

Excited!

avatar image bustedkrutch · Aug 30, 2013 at 04:38 PM 2
Share

Thank you both, @perchik and @gheeler.

@gheeler - you are correct sir! I needed to use the a "new" type before adding it to the List. BTW, can you add an answer so that I can indicate it as an answer. We are currently only in the comments section and I'd like to give you the karma you're due :)

@perchik - you are right that I had used the "new" for xport, however I used it only once in Start, which totally makes sense now that all of the output was referencing the exact same object.

So here was the solution that @gheeler was writing about:

         var xport = new Transport();
         xport.confirmation     = confirmation;
         xport.position        = loc;
         transport_list.Add(xport);
avatar image Eric5h5 · Aug 30, 2013 at 04:52 PM 0
Share

Just a comment about your constructor for the Transport class, namely that it's not doing anything, since those are the default values anyway. It would be a lot more useful to have a constructor that takes user-supplied values.

 public class Transport{
     var position : Transform;
     var confirmation : int;
     var up : boolean;
     var down : boolean;
     var attempts : int;
  
     public function Transport(position : Transform, confirmation : int){
        this.position = position;
        this.confirmation = confirmation;
     }
 }

Then you can just do var xport = new Transport(loc, confirmation);.

Show more comments

1 Reply

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

Answer by gheeler · Aug 30, 2013 at 04:01 PM

i dont know js as well as c# but when youre adding to the list, you need to make sure its a new object rather than changing the reference and just passing that.

in c# anyway there'd be a need for the 'new' keyword

although i think but im not sure if your transport class was a struct instead it would work because structs are value types compared to class being a reference type

Comment
Add comment · Show 1 · 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
avatar image bustedkrutch · Aug 30, 2013 at 09:56 PM 0
Share

So here was the solution that @gheeler was writing about:

    var xport = new Transport();
    xport.confirmation    = confirmation;
    xport.position       = loc;
    transport_list.Add(xport);

The line with "var xport = new Transport();" needed to be added each time I added to the list rather than in my original code that only created a single instance of xport in the Start function.

Again, thanks gheeler!

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

20 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

Related Questions

Add Item To A List Of Classes 0 Answers

Lists within lists to make a crafting system 1 Answer

Access list storing custom class variables from another script 1 Answer

How to carry List's and Vector3's to other classes? Interfaces don't work? 4 Answers

How to access individual class elements, that has an array, that is in a list for c# 1 Answer


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