• 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 gregzo · Aug 02, 2012 at 10:55 AM · listvector2

List of Vector2 - Madness looms in simple script

Hi to all!

I'm about to go crazy. Trying to store values in a list of Vector2, and it's just not working. Here's a script demonstrating the issue:

 function Test()
 {
     var arr = new float[100];
     for (val in arr)
     {
        val = Random.value;
     }
  
     var tempMaxList = List.<Vector2>();
     tempMaxList.Add(Vector2.zero);
  
     for(var i:int=0;i<arr.length;i++)
     {
        if(arr[i]>tempMaxList[0].y)
        {
           tempMaxList[0].x = i;
           tempMaxList[0].y = arr[i]; 
           Debug.Log(tempMaxList[0]+" "+i+" "+arr[i]); //tempMaxList[0] is 0,0, WTF???
        }
     }
 }

If I replace tempMaxList by a simple Vector2 var, everyt$$anonymous$$ng is fine.

I'm sure epic face palming will follow, but I just can't see what I'm doing wrong here...

Comment
Add comment · Show 1
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 gregzo · Aug 02, 2012 at 11:16 AM 0
Share

1 Reply

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

Answer by Bunny83 · Aug 02, 2012 at 11:39 AM

First t$$anonymous$$s usually doesn't work:

 var arr = new float[100];
 for (val in arr)
 {
    val = Random.value;
 }

I don't use UnityScript, but a foreach / for-in loop uses an IEnumerator to iterate through the array. You can never assign values to the iterator variable since it's always just a copy of the value stored in the array. Use a normal for loop:

 var arr = new float[100];
 for (var n = 0; n< arr.Length; n++)
 {
    arr[n] = Random.value;
 }

Second, your problem that the returned values are 0,0 is that Vector2 is a value type. When you use the indexer of your List you get a copy of the item. Changing the members of t$$anonymous$$s copy won't change the item in the list.

Use either :

 for(var i:int=0;i<arr.length;i++)
 {
    if(arr[i]>tempMaxList[0].y)
    {
       tempMaxList[0] = new Vector2(i,arr[i]);
    }
 }

or

 for(var i:int=0;i<arr.length;i++)
 {
    if(arr[i]>tempMaxList[0].y)
    {
       var tmp = tempMaxList[0];
       tmp.x = i;
       tmp.y = arr[i];
       tempMaxList[0] = tmp;
    }
 }

Lastly, I don't get why you use a List. You just add one item and always use t$$anonymous$$s like a single variable. So the whole t$$anonymous$$ng could be done like t$$anonymous$$s:

 function Test()
 {
     var arr = new float[100];
     for (var n = 0; n< arr.Length; n++)
     {
        arr[n] = Random.value;
     }
 
     var tempMax = Vector2.zero;
 
     for(var i = 0; i < arr.length; i++)
     {
        if(arr[i] > tempMax.y)
        {
           tempMax.x = i;
           tempMax.y = arr[i]; 
           Debug.Log(tempMax + " " + i + " " + arr[i]);
        }
     }
 }
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 gregzo · Aug 02, 2012 at 11:47 AM 0
Share
avatar image Bunny83 · Aug 02, 2012 at 11:56 AM 1
Share
avatar image Eric5h5 · Aug 02, 2012 at 06:52 PM 1
Share

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

8 People are following this question.

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

Related Questions

A node in a childnode? 1 Answer

How to make an IList of a certain type? 2 Answers

Find the highest first value of a vector2 in a list? 2 Answers

How to read X and Y component ( of a vector2) from a list 1 Answer

drag drop a object into list objectes 0 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