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 /
This question was closed Aug 15, 2020 at 12:21 PM by Quadovc for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Quadovc · Aug 15, 2020 at 11:32 AM · arraylistarraysclasslists

Problem with arrays in a list

I have made a class that contains an array of Vector3s and I try to add that array in a list. Nothing I've tried to do seems to solve this. I get an error every time that array is out of range. Any ideas?

     [System.Serializable]
     public class NodeClass
     {
         public Vector3[] location;
     }

     public List<NodeClass> allNodes;

     parts = GameObject.FindGameObjectsWithTag("Part");
     for (int i = 0; i < parts.GetLength(0); i++)
     {
         GameObject go = parts[i];
         Part part = go.GetComponent<Part>();
         Vector3[] nodes = part.nodeLocation;

         allNodes[i] = new NodeClass();

         for (int x = 0; x < nodes.GetLength(0); x++)
         {
             allNodes[i].location[x] = nodes[x];
         }
     }
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

  • Sort: 
avatar image
1
Best Answer

Answer by Spip5 · Aug 15, 2020 at 11:59 AM

You script does not make much sense :P. At not point you add your nodes to your list, and i am confused as to why you are looping through your list again (inside your node loop) to basically re-put the same value ? Something like this would work, I guess ?

 [System.Serializable]
     public class NodeClass
     {
         public Vector3[] location;
 
         public NodeClass(Vector3[] _locations)
         {
             location = _locations;
         }
     }
 
 [...]
 
     void test()
     {
 
      List<NodeClass> allNodes = new List<NodeClass>();
 
      GameObject[] parts = GameObject.FindGameObjectsWithTag("Part");
 
          for (int i = 0; i<parts.GetLength(0); i++)
          {
             GameObject go = parts[i];
             Part part = go.GetComponent<Part>();
             Vector3[] nodes = part.nodeLocation;
 
             allNodes.Add(new NodeClass(nodes));
          }
     }
 }
 
 public class Part
 {
     public Vector3[] nodeLocation;
 }
 

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 Quadovc · Aug 15, 2020 at 12:08 PM 0
Share

Exactly what I was trying to do. I'm still a bit beginner with C# and Unity so don't really understand how these things operate. Thank you!

avatar image Spip5 Quadovc · Aug 15, 2020 at 01:44 PM 0
Share

No worries, everyone's a beginner at some point :). Unless you are adding other values in your NodeClass, you can even simplify your script more by making a list of Vector3 arrays directly, and not a list of NodeClasses

         List<Vector3[]> allNodes = new List<Vector3[]>();
 

Follow this Question

Answers Answers and Comments

152 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

Related Questions

How can you do calculations on two lists? 1 Answer

Undo/back system using a List/Array 2 Answers

Reading all data in the list with duplicate data inside 1 Answer

How to return index of List element? 1 Answer

How to modify array values? 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