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 tschwartzenburg99 · Dec 03, 2020 at 01:20 PM · buglistdebuglists

List values change for no apparent reason

Hi there, this is my first post on here, so sorry if I do not follow a proper format or something.

Essentially, I have two lists of GameObjects. Both lists are the same length. One list is a reference and the other list (let's call this the normal list) are those same elements but in a different order. What I want to do is to match the positions of each element in the normal list to the position of the same-indexed element in the reference list.

What I have written already works perfectly except for two GameObjects. For some reason in my first and second loop of the for-loop, two positions in my reference list just change for no reason that I can tell.

Here is my code:

 private void rotate(List<GameObject> cubeList)
   {
     //make a copy of cubeList to reference
     List<GameObject> referenceList = copyList(cubeList);
 
     //change the cube list
     //get temporary values of the first corner and first middle
     GameObject tempCorner = cubeList[cubeList.Count - 2];
     GameObject tempMiddle = cubeList[cubeList.Count - 1];
     
     //move the corners and middles accordingly in the list
     for(int i = cubeList.Count - 1; i > 1; i--)
     {
       cubeList[i] = cubeList[i - 2];
     }
     //change the first and second cube spots
     cubeList[0] = tempCorner;
     cubeList[1] = tempMiddle;
 
     //actually move the cubes now by making the cube list position equal to the reference list position
     for(int i = 0; i < cubeList.Count; i++)
     {
       cubeList[i].transform.position = referenceList[i].transform.position;
       cubeList[i].transform.Rotate(new Vector3(0, 0, -90));
     }
   }

 private List<GameObject> copyList(List<GameObject> list)
   {
     List<GameObject> copyList = new List<GameObject>(); 
 
     for(int i = 0; i < list.Count; i++)
     {
       copyList.Add(list[i]);
     }
 
     return copyList;
   }

Here are some of my debugger statements, so you can see what I am talking about.

 Proper Reference List: 
 (-1.0, 1.0, -1.0), (0.0, 1.0, -1.0), (1.0, 1.0, -1.0), (1.0, 0.0, -1.0), (1.0, -1.0, -1.0), (0.0, -1.0, -1.0), (-1.0, -1.0, -1.0), (-1.0, 0.0, -1.0), 
 
 Pass 0:
 Reference List:
 (-1.0, 1.0, -1.0), (0.0, 1.0, -1.0), (1.0, 1.0, -1.0), (1.0, 0.0, -1.0), (1.0, -1.0, -1.0), (0.0, -1.0, -1.0), (-1.0, 1.0, -1.0), (-1.0, 0.0, -1.0), 
 Cube List: 
 (-1.0, 1.0, -1.0), (-1.0, 0.0, -1.0), (-1.0, 1.0, -1.0), (0.0, 1.0, -1.0), (1.0, 1.0, -1.0), (1.0, 0.0, -1.0), (1.0, -1.0, -1.0), (0.0, -1.0, -1.0), 
 
 Pass 1:
 Reference List:
 (-1.0, 1.0, -1.0), (0.0, 1.0, -1.0), (1.0, 1.0, -1.0), (1.0, 0.0, -1.0), (1.0, -1.0, -1.0), (0.0, -1.0, -1.0), (-1.0, 1.0, -1.0), (0.0, 1.0, -1.0), 
 Cube List: 
 (-1.0, 1.0, -1.0), (0.0, 1.0, -1.0), (-1.0, 1.0, -1.0), (0.0, 1.0, -1.0), (1.0, 1.0, -1.0), (1.0, 0.0, -1.0), (1.0, -1.0, -1.0), (0.0, -1.0, -1.0), 
 
 Pass 2:
 Reference List:
 (1.0, 1.0, -1.0), (0.0, 1.0, -1.0), (1.0, 1.0, -1.0), (1.0, 0.0, -1.0), (1.0, -1.0, -1.0), (0.0, -1.0, -1.0), (-1.0, 1.0, -1.0), (0.0, 1.0, -1.0), 
 Cube List: 
 (-1.0, 1.0, -1.0), (0.0, 1.0, -1.0), (1.0, 1.0, -1.0), (0.0, 1.0, -1.0), (1.0, 1.0, -1.0), (1.0, 0.0, -1.0), (1.0, -1.0, -1.0), (0.0, -1.0, -1.0), 
 
 Pass 3:
 Reference List:
 (1.0, 1.0, -1.0), (1.0, 0.0, -1.0), (1.0, 1.0, -1.0), (1.0, 0.0, -1.0), (1.0, -1.0, -1.0), (0.0, -1.0, -1.0), (-1.0, 1.0, -1.0), (0.0, 1.0, -1.0), 
 Cube List: 
 (-1.0, 1.0, -1.0), (0.0, 1.0, -1.0), (1.0, 1.0, -1.0), (1.0, 0.0, -1.0), (1.0, 1.0, -1.0), (1.0, 0.0, -1.0), (1.0, -1.0, -1.0), (0.0, -1.0, -1.0), 

and so forth.

Pass 0 and Pass 1 are the only things that don't work (look at the last two positions, they change on the reference list when they shouldn't).

I am completely stumped. I have been trying other ways, really all I can think of, but referenceList continues to be altered. It is baffling me. Any help would be appreciatedQ

Comment
Add comment · Show 1
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 sacredgeometry · Dec 03, 2020 at 08:04 PM 0
Share

Your code is a little convoluted. How about describing what is it you are trying to do?

Can you writer an example and a use case for your code please?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by UnderShad · Dec 03, 2020 at 09:17 PM

Try using List<GameObject> referenceList = new List<GameObject>(cubeList); instead of copyList . For me list are notoriously using reference instead of copy.

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 tschwartzenburg99 · Dec 03, 2020 at 10:43 PM 0
Share

Hey! So unfortunately that didn't work. Any other suggestions? Any idea of how my reference list is being altered? I appreciate your help!

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

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

A node in a childnode? 1 Answer

Why is my list empty? (c# question) 1 Answer

Check if Exists and if so, Add it to Other List 1 Answer

Why is my list not adding? 1 Answer

How to find all similar elements in a list? 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