Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 vvcc · Mar 12, 2018 at 11:47 PM · javascriptarrayupdatetimepush

Updating an array indices dynamically

All,

I have a script that records the time and 3D transform positions since game load. I want to end up with an array like this, which records all of the positions and timestamps collected for the positions until it reaches a certain position:

 myArray =[
 {positionX : #, positionY: # , positionZ : #, timeStamp: #},
 {positionX : #, positionY: # , positionZ : #, timeStamp: #},
 etc...];

Here is what I've done to take a stab at this, shortened for easier read:

 var myObject : GameObject;
 var myArray = new Array ();
 var seconds;
 
 function Update(){
     
 //stuff here to find seconds, deleted to save space
     
 for (var i=1;i<transformarray.length;i++){
     myArray[i]={};
     myArray[i].positions=myObject.transform.localPosition; //here, positionX, etc
     myArray[i].timeStamp =seconds;
 };
   
 if (myObject.transform.localPosition.x = 1 &&myObject.transform.localPosition.z = 3 &&myObject.transform.localPosition.y = 2) {    
   stop updating array
 Debug.Log(myArray);

 }
 }


Instead, I get

 Debug.Log[Object[]);


Could someone help me with this?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Zodiarc · Mar 13, 2018 at 03:10 PM

I'd rather do an array of specialized objects instead of an arbitrary array of arrays:

 public class PositionWithTimestamp {
     public Vector3 position;
     public int timestamp
     public PositionWithTimestamp (Vector3 position, int timestamp) {
         this.position = position;
         this.timestamp = timestamp;
     }
 }

then you can fill it dynamically like this:

 List<PositionWithTimestamp> positions = new List<PositionsWithTimestamp>();
 
 if(transform.position.x != 1 && transform.position.y != 2 && transform.position.z != 3) { // choose your values as you need
     positions.Add(new PositionsWithTimestamp(transform.position, seconds));
 }

This way you will be adding your positions as long as you don't stand in that specific position. To stop all adding you could do the following:

 bool addPositions = true;
 
 if(transform.position.x != 1 && transform.position.y != 2 && transform.position.z != 3) { // choose your values as you need
         addPositions = false;
     }
 
 if(addPositions){
     positions.Add(new PositionWithTimestamp(transform.position, seconds));
 }

All assuming that you're doing it in Update()

Also I'd recommend to switch to C# since UnityScript is deprecated and this will bite you in the a$$ later.

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 vvcc · Mar 14, 2018 at 03:43 PM 0
Share

Thank you, I managed to get it to work with this help. Yea I do need to get into C#, I got into UnityScript too deep

avatar image
1

Answer by Summit_Peak · Mar 13, 2018 at 12:13 AM

Try using == instead of =

if (myObject.transform.localPosition.x = 1

if (myObject.transform.localPosition.x == 1

Also, try looking at an array element or value instead:

Change:

Debug.Log(myArray);

To:

Debug.Log(myArray[0]);

Debug.Log(myArray[0].timestamp);

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 vvcc · Mar 13, 2018 at 02:27 PM 0
Share

@Summit_Peak thank you for your advice. I've tried both, changing = to ==, and also trying to look at the element or value. I'm not sure if it is working, as I still get Object[] as an output when I look at the array itself, or if I look at myArray[0], it says that what I'm looking for is outside the length of the array.

avatar image vvcc · Mar 13, 2018 at 02:46 PM 0
Share

I've added a print(i) in the for loop, and i remains at 1 when I take it outside of the Update() function, and fluctuates between 1 and 2. This could be a hint...I'll play around and update if I can manage to find the answer.

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

129 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

Related Questions

Time.deltaTime not consistent over time 1 Answer

Constant Yield Time 1 Answer

Push Custom Class to Array (JavaScript) 1 Answer

Array.Push() for Vector3[] or how to add items to Vector3 array without knowing index 1 Answer

Getting a variable from a GameObject inside a 2d array? 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