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 fjalla · Sep 13, 2013 at 03:22 PM · arrayclassloopsconstructors

Losing array in class after Array.ToBuiltin?

Hi. Got a problem.

I've got this code (troll is a type of tank if anyone asks):

 class Indinces {
     
     var i = new Array ();
     
     function Indinces (things : GameObject[], startIn : Array) {
         
         if (things.Length > 0) {
             for (thing in things) {
                 var thingVar = Diggers (thing);
                 i.Push (thingVar);
             }
             
             for (t = 0; t < i.length; t++) {
                 for (sv in startIn) {
                     i[t].v.Add (sv);
                 }
             }
         
         }
     }
 
 }
             
             
 class Diggers {
 
     var o : GameObject;
     var v = new Array ();
     var e = new Array ();
     
     function Diggers (oo : GameObject) {
         
         o = oo;
     }
 
 }


 function Update () {
     
     tanks = GameObject.FindGameObjectsWithTag ("Tank");
     trollRockets = GameObject.FindGameObjectsWithTag ("RocketTroll");
     
     var indinces = new Array (); //contains ints
     
     var iTanks = Indinces (tanks, indinces);

     var iRockets = Indinces (trollRockets, indinces);

     for (/*stuff*/) {
 
         var iTanks2 : Diggers[] = iTanks.i.ToBuiltin (Diggers) as Diggers[];

         for (tankClear in iTanks.i) {    
             tankClear.v.Clear ();
         }
         
         var iRockets2 : Diggers[] = iRockets.i.ToBuiltin (Diggers) as Diggers[];

         for (rocketClear in iRockets.i) {    
             rocketClear.v.Clear ();
         }


             //this is where problems start*
 

*after this, the "o" object is still there, but the "v" array is empty. Maybe a chance that it was lost while Array.ToBuiltin ()?

Thanks for reading this far.

EDIT: Same problem with Generic Lists.

Comment
Add comment · Show 8
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 vexe · Sep 13, 2013 at 03:34 PM 0
Share
 o = oo;

Very expressive, self-explanatory na$$anonymous$$g :) - If you keep it like that it won't be long till you get your self and your $$anonymous$$m lost and confused working on a big project. Just a side tip: Use self-explanatory names, saves you a lot of headache :)

avatar image fjalla · Sep 13, 2013 at 03:53 PM 0
Share

the best thing to do if you want another one-letter variable is to type that letter twice :D btw, o as object, v as vertex, e as explosion. Its not hat confusing :)

avatar image vexe · Sep 13, 2013 at 03:59 PM 1
Share

"the best thing to do if you want another one-letter variable is to type that letter twice" lol, good one.

It's better to be crystal clear than trying to guess what something is. It might be O$$anonymous$$ here (a little bit O$$anonymous$$) when your scripts are not very big, but when they do get big, when you deal with your $$anonymous$$m, and even when you get back to your code after a while it will look Babylon text :D

It will be very hard for you to remember what you meant with your letters :) - Not to mention, good na$$anonymous$$g make documentation short, if the name is clear, then the purpose is clear.

A good programmer writes well documented code, but yet a better one lets his code speak for itself, without any documentation! Wouldn't that be nice? :)

avatar image fjalla · Sep 13, 2013 at 04:47 PM 0
Share

aren't you a poet? but I suppose yeah, I should add comments after variable to know what they are (if thats what you meant)

avatar image Eric5h5 · Sep 13, 2013 at 04:52 PM 0
Share

Never use the Array class under any circumstances. Ins$$anonymous$$d of this array conversion stuff, just use a generic List.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ArkaneX · Sep 14, 2013 at 12:50 PM

Converting to builtin array does not mean the array elements are cloned - they are still the same objects. So first element in iTanks.i javascript Array is exactly the same element as first element of iTanks2 .NET array.

You can't then call v.Clear() on each element of both javascript arrays, and expect that it won't affect elements of newly created builtin arrays.

EDIT: you can check that by examining v.length after calling ToBuiltin but before Clear calls.

Comment
Add comment · Show 2 · 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 fjalla · Sep 15, 2013 at 05:12 PM 0
Share

Actually, that sucks. One would think that converting an array will solve the problem, but no, it's a shallow copy again. Well, thanks anyway :). I already fixed it, more complicated way that ToBuiltin (), I'll maybe post it later.

avatar image ArkaneX · Sep 15, 2013 at 05:25 PM 0
Share

It's not a shallow copy, it's just the same reference. Shallow copies are new objects.

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

19 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

Related Questions

Multidimensional Arrays of Classes in Java 2 Answers

How to create Random Stats matching the weapons? 0 Answers

"Null Reference Error" when using a custom class as an array 1 Answer

Error message when accessing class array 1 Answer

Accessing variables and arrays from inside classes? 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