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 ronronmx · Jan 14, 2011 at 07:17 PM · arraystructcollections

Accessing struct variables

SO I have been learning about structs and Lists, and I have a questions about ways to access a struct variable. Below is the original code (Thanks a bunch to Jessy for answering my other question and giving me this great example):

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class ObjectList : MonoBehaviour {

 struct CloneChildAtStart {

     public Vector3 clonePosition;
     public Quaternion cloneRotation;

     public CloneChildAtStart (Vector3 clonePosition, Quaternion cloneRotation) {
         this.clonePosition = clonePosition;
         this.cloneRotation = cloneRotation;
     }

 }

 List<List<CloneChildAtStart>> clonesChildrenAtStart;


 void Start () {
     clonesChildrenAtStart = new List<List<CloneChildAtStart>> ();
     List<CloneChildAtStart> cloneChildrenAtStart = new List<CloneChildAtStart> ();
     foreach (GameObject clone in GameObject.FindGameObjectsWithTag ("Clone")) {
         cloneChildrenAtStart.Clear ();
         foreach (Transform child in clone.transform) {
             cloneChildrenAtStart.Add (new CloneChildAtStart (child.position, child.rotation));
             //Debug.Log(cloneChildrenAtStart.Count);
         }
         clonesChildrenAtStart.Add (cloneChildrenAtStart);
     }
 }

 void Update () {
     for (int i = 0; i < clonesChildrenAtStart.Count; ++i) {
         for (int j = 0; j < clonesChildrenAtStart[i].Count; ++j) {
             //Debug.Log (clonesChildrenAtStart[i][j]);
         }
     }
 }

}

I'm trying to figure out the different ways to use a struct, so here what I came up with below, and I want to know if the way I'm accessing my struct variables here is basically the same as the code above?

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class ObjectList : MonoBehaviour {

 struct CloneChildAtStart {

     public Vector3 clonePosition;
     public Quaternion cloneRotation;

 }
 CloneChildAtStart sceneObjects = new CloneChildAtStart ();


 List<List<CloneChildAtStart>> clonesChildrenAtStart;


 void Start () {
     clonesChildrenAtStart = new List<List<CloneChildAtStart>> ();
     List<CloneChildAtStart> cloneChildrenAtStart = new List<CloneChildAtStart> ();
     foreach (GameObject clone in GameObject.FindGameObjectsWithTag ("Clone")) {
         cloneChildrenAtStart.Clear ();
         foreach (Transform child in clone.transform) {
             sceneObjects.clonePosition = child.position;
             sceneObjects.cloneRotation = child.rotation;
             cloneChildrenAtStart.Add (sceneObjects);
             //Debug.Log(cloneChildrenAtStart.Count);
         }
         clonesChildrenAtStart.Add (cloneChildrenAtStart);
     }
 }

 void Update () {
     for (int i = 0; i < clonesChildrenAtStart.Count; ++i) {
         for (int j = 0; j < clonesChildrenAtStart[i].Count; ++j) {
             //Debug.Log (clonesChildrenAtStart[i][j]);
         }
     }
 }

}

Thanks in advance!!! Stephane

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

· Add your reply
  • Sort: 
avatar image
0

Answer by ronronmx · Jan 15, 2011 at 06:36 PM

Jessy, thanks a lot for all this information, it is really helpful. I'm not yet very comfortable with constructors as well, which is why I tried to use your code in another way like above, so that I can understand better how to use constructors, and it makes more sense now. Basically what I'm doing is instantiating and destroying objects as I go, kinda like streaming, so I store their starting positions, then store the finish line position, and instantiate them in front of me as I drive by adding their starting position + finish line position * by the lap number. The stored object positions never change, but the instantiated ones do...so I think a class is better suited for my needs then a struct if I understand correctly, since I do manipulate the position and rotation variables every time I instantiate one of the stored object.

And also thanks for the tips on naming conventions. I always wonder how to name my variables :) Your help is very appreciated!

Comment
Add comment · 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

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

No one has followed this question yet.

Related Questions

Array with multiple variables in Inspector 1 Answer

How do I delete or empty an array of custom structs during edit mode? 1 Answer

Dimentional struct matrix 0 Answers

Selecting Structs from dropdown menu in inspector. 1 Answer

Multidimensional array of structs 2 Answers


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