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 11, 2011 at 05:10 AM · arrayarraylist

Populate array on Start() and accessing it on Update()...how to?

So I'm a little stuck on this one... I'm populating an array of children and storing their starting position in the Start() function, but when I try to access the position of each of them in the Update() function, it only returns the position of the last object in the array.

Here's my code below:

using UnityEngine; using System.Collections;

// Remove warnings... #pragma warning disable 0219

public class ReloadTrack : MonoBehaviour {

 // Define variables...
 private Transform bike;
 private float bikeCurrentPos;

 private Vector3 clonePosition = new Vector3 (0,0,0);
 private Quaternion cloneRotation;
 public Vector3 cloneDistance;
 private bool cloneExist;

 private Vector3 finishPos;
 public int numberOfLaps = 5;
 private int currentLap;

 // Define arrays...
 private GameObject[] sceneObjects;
 private Transform[] allChildren;

 void Start () {

     // Cache bike transform...
     bike = GameObject.FindWithTag ("Bike").transform;

     // Cache finish position...
     finishPos = transform.position;

     // Start at lap 1...
     currentLap = 1;

     // Cache all parent gameObjects in scene which have the "Clone" tag
     sceneObjects = GameObject.FindGameObjectsWithTag ("Clone");

     foreach (GameObject go in sceneObjects) {

         allChildren = go.GetComponentsInChildren<Transform> ();

         for (int i = 1; i < allChildren.Length; i++) {
             clonePosition = allChildren[i].position;
             cloneRotation = allChildren[i].rotation;                
         }
     }
 }


 void Update () {

     foreach (GameObject go in sceneObjects) {
         allChildren = go.GetComponentsInChildren<Transform> ();
         for (int i = 1; i < allChildren.Length; i++) {
             Debug.Log (clonePosition);
         }
     }

}

So "clonePosition" in Start() returns the position of every object in the array "allChildren[i]", but when I debug it in Update(), it returns the position of the last object it cached on Start() for every single object.

My question is, how can I define "clonePosition" in Start() and access all its values in Update() without having to define it again? Thanks!

Comment
Add comment · Show 2
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 Jessy · Jan 11, 2011 at 08:05 AM 0
Share

Why are you using System.Collections?

avatar image ronronmx · Jan 14, 2011 at 07:05 PM 0
Share

Not sure, I see it in most scripts so I just put it in $$anonymous$$e :) I don't know much about it so I copy what I see. Why shouldn't I use it?

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Jessy · Jan 11, 2011 at 09:09 AM

clonePosition is a Vector3. You need some kind of proper storage for it (array / List); a Vector 3 is not appropriate, because you're just overwriting it a whole bunch with the code you have. Here's one way to go about it:

struct CloneChildAtStart { Vector3 position; Quaternion rotation;

 public CloneChildAtStart (Vector3 position, Quaternion rotation) {
     this.position = position;
     this.rotation = rotation;
 }

} 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)); 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]); }

Comment
Add comment · Show 4 · 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 yoyo · Jan 11, 2011 at 09:12 AM 0
Share

I don't think the OP is trying to store all the positions, just extract them one at a time for logging. (I could be wrong ...)

avatar image ronronmx · Jan 12, 2011 at 07:04 PM 0
Share

Actually yes I am trying to store all the positions, so that I can later extract them without having to write the same for loops in Update.

I'm rather new to coding, so I'm not yet familiar with "struct" and "List" so I'm gonna look into it and try to understand Jessy's code above and use it for my needs, because from what I understand so far it is exactly what I need to do. Thanks!!!

avatar image Jessy · Jan 12, 2011 at 08:00 PM 0
Share

A struct is a compact data structure, well-suited to storing unchanging data, like I think you're asking for here. A List is like an array, but generally much easier to work with. I made a List of Lists in the code above, because you need a list of "clones", each with their associated children.

avatar image ronronmx · Jan 13, 2011 at 04:45 AM 0
Share

Great, thanks a lot for the explanation Jessy!

avatar image
1

Answer by yoyo · Jan 11, 2011 at 09:11 AM

You just need to add this in your second loop (in Update) ...

clonePosition = allChildren[i].position;

Also note that arrays in C# are indexed from 0, not 1, so your loop counters need to start at 0.

(And since you use allChildren and clonePosition in local scope, they don't need to be declared at the script level, but could be just local variables inside Start and Update.)

Comment
Add comment · Show 3 · 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 Jessy · Jan 11, 2011 at 09:18 AM 1
Share

I figured he was starting at 1, because index 0 is the Transform itself, when you use GetComponentsInChildren.

avatar image yoyo · Jan 11, 2011 at 03:34 PM 0
Share

Good catch, I didn't think of that.

avatar image ronronmx · Jan 12, 2011 at 06:59 PM 0
Share

Yes I do start at 1 to skip the Transform itself ;)

avatar image
0

Answer by ronronmx · Jan 19, 2011 at 09:24 PM

So after multiple (failed) attempts and lots of trial and error, I realized I was having the same problem with the code above as I did with my old code.

It stores all of the children's positions and rotations in Start, but when I try to access them in Update, "clonesChildrenAtStart[i][j]" only returns the last stored child and not all of them.

It won't let me access child 1-2-3-4....unless I write the same foreach loop in update as I did on Start, which is exactly what I was trying to avoid.

I'm pretty confused at this point, I'm not sure I'm expressing what I need correctly and I don't know what else to try. I don't understand how I can extract all the stored positions one at a time in Update, without having to store them beforehand in Update, but only in Start?

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
avatar image
0

Answer by pixelmixer · Jan 19, 2011 at 10:23 PM

The reason you're only seeing the last child when you Log clonePosition is because in your Start loop each time it loops through you are overwriting the previously set clonePosition value. This essentially negates anything you are doing in your Update loop, and thus won't work.

I would store that clonePosition value in another array, then loop through that array later in your Update.

For example...

private Vector3 clonePosition[]; private Vector3 cloneRotation[];

foreach (GameObject go in sceneObjects) { allChildren = go.GetComponentsInChildren<Transform> (); for (int i = 1; i < allChildren.Length; i++) { clonePosition.Add(allChildren[i].position); cloneRotation.Add(allChildren[i].rotation);
} }

then later in Update you can do something like this...

foreach (Vector3 vect in clonePosition) { Debug.Log(vect); }

foreach (Vector3 vect in cloneRotation) { Debug.Log(vect); }

You could even go further and optimize that more to include Position and Rotation in a multidimensional array in order to access it later in Update.

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 ronronmx · Jan 19, 2011 at 11:26 PM 0
Share

Pixelmixer, thanks a lot for your help. About the multidimensional array, I think that's what Jessy try to do with his code above, by creating a "List", where "ChildObj" is a struct...but I'm not yet very familiar with structs and classes, so ho do you suggest I create a multidimensional array (or List) using a struct to store position and rotation?

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

Collections don't work! 1 Answer

Storing Level Data in Array of Array (grid-based game) 1 Answer

Question about updating particles in an arraylist 0 Answers

How to get smallest element and key from int list 1 Answer

Print arraylist values to console 4 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