- Home /
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
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!