• 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 /
  • Help Room /
avatar image
0
Question by HardRockz · Sep 27, 2016 at 10:42 AM · arrays

Unity C# Transform array returns only last value

Hi guys,

I have a list of object positions in a Dictionary and i am trying to create Transform array using those positions. Below is my code. RetTransform is a public game object and i am changing the x position of RetTransform game object and then adding it to the myTransformArray using foreach loop.

public Transform[] getAllTransfromVal(int arrayNo) {

     int countArray = 0;
     if (arrayNo == 1) {
         int arrCount = timeStampDataArray.Count;
         private Transform[] myTransformArray = new Transform[arrCount];
         int[] xxx = new int[arrCount];
         foreach (KeyValuePair<int,timeStampData> pair in timeStampDataArray) {
             Vector3 temp = RetTransform.transform.position;
             temp.x = pair.Value.OBJPos;
             RetTransform.transform.position = temp;
             myTransformArray [countArray] = RetTransform.transform;
             print ("key" +countArray +" x " + myTransformArray [countArray].position.x + " y " + myTransformArray [countArray].position.y + " z " + myTransformArray [countArray].position.z);
             countArray++;

         }

         for (int k = 0; k < myTransformArray.Length; k++) {
             print ("==== " + k +"===== "+myTransformArray [k].position.x);
         }



In the first print statement it prints values like below which is correct.

key0 x -29 y 1.8 z -4.3
key1 x -25 y 1.8 z -4.3
key2 x -35 y 1.8 z -4.3

But in the second print statement it prints last x value 3 times

==== 0===== -35
==== 1===== -35
==== 2===== -35

Need some help guys.Thanks in advance :-)

Comment
Add comment · Show 6
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 doublemax · Sep 27, 2016 at 11:23 AM 0
Share
 myTransformArray [countArray] = RetTransform.transform;

This line doesn't copy the data, it just stores a reference to RetTransform.transform. And as that changes on each iteration of the loop, you'll get the last value three times.

But i can't tell you how to force a deep copy of the transform.

Do you really need an array of Transforms? If you need only the positions use an array of Vector3.

avatar image HardRockz · Sep 27, 2016 at 11:28 AM 0
Share

@doublemax Thank you for pointing that out :-) I am using that Transform array to move my object using Nav $$anonymous$$esh Agent using waypoints. Any suggestions ?

avatar image Bonfire-Boy HardRockz · Sep 27, 2016 at 12:22 PM 0
Share

If you need it to be an array of Transforms rather than just positions as doublemax suggested, one option would be to create new ones for the purpose.

So for example, ins$$anonymous$$d of setting RetTransform's position and then setting the array element to its Transform, you could clone RetTransform each time, set the clone's position and assign the clone to the array element. I guess there might be a bunch of other stuff you'd need, managing these objects, but I think the basic idea could be the basis of a solution.

avatar image HardRockz · Sep 27, 2016 at 12:20 PM 0
Share

I used Vector3 array ins$$anonymous$$d of Transform array as you @doublemax suggested.Thank you :-)

avatar image Nestoraj HardRockz · Sep 27, 2016 at 02:20 PM 0
Share

You can store all the data from a Transform in a single Vector3 array using this:

Vector3[] transformData = new Vector3[3]; transformData[0] = RetTransform.transform.position; transformData[1] = RetTransform.transform.rotation.eulerAngles; transformData[2] = RetTransform.transform.localScale;

and then you can get the data back using:

YourNewObject.transform.localPosition = transformData[0]; YourNewObject.transform.localRotation = Quaternion.Euler(transformData[1]); YourNewObject.transform.localScale = transformData[2];

I hope it helps you =D

avatar image HardRockz Nestoraj · Sep 27, 2016 at 02:24 PM 0
Share

Thank you @Nestoraj

I will take a look at it

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why does this loop throw an error if int questComplete rises above 1? 0 Answers

Managing In-Game Variables of Different Data Types 1 Answer

Defining a number of gameobjects from another gameobject 0 Answers

animation array trying to play all at once 0 Answers

Cannot Set Array Size in Inspector 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