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 cidmodder · Jan 23, 2012 at 01:35 AM · c#list

How do you get a certain index of a List

I have a list of vector3s and I want to go through and multiply each vector3 by a random.value but when ever I do my script i get the error that the list is a field when type was expected. Here is my script:

     using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class MeshEditing1 : MonoBehaviour {
     public Vector3[] vertices;
     public Vector3[] normals;
     public System.Collections.Generic.List<Vector3> firststep;
     public System.Collections.Generic.List<Vector3> underWater;
     public Vector3[] aboveWater;
     public Mesh mesh;
     public float distance;
     public int f;
     
     // Use this for initialization
     void Start () {
     mesh = GetComponent<MeshFilter>().mesh;
     vertices = mesh.vertices;
     normals = mesh.normals;
         int i = 0;
         while(i < vertices.Length)
         {
         var percent = Random.value;
         var number= (1 + percent)/85;
         //vertices[i] += normals[i]* number;
         var positionPercent = Random.value;
         int x = i+1;
         
         if (percent >= .50f)
             {
                 distance= vertices[i].magnitude - vertices[x].magnitude;
                 //distance = Mathf.Pow( Mathf.Pow((vertices[i].x - vertices[x].x),2) + Mathf.Pow((vertices[i].y - vertices[x].y),2) + Mathf.Pow((vertices[i].z - vertices[x].z),2),1/2);
                 if (distance < .5f)
                 {
                 firststep.Add(vertices[x]);
                 x++;
                 percent = Random.value;
                 }
                 
                 
             }
         if (percent < .50f)
             {
             underWater.Add(vertices[i]);    
             percent = Random.value;
             i= x+1;    
             }
         
     
         }
         
     vertices = firststep.ToArray();
         //aboveWater = firststep.ToArray();
         for(int f = 0; f < firststep.Count; f++)
         {
             var number = 1+ Random.value;
             vertices[f] *= number;
                         
         }
     **mesh.vertices= firststep;**
     
     }
     
     // Update is called once per frame
     void Update () {
         
     }
 }

any ideas?

EDIT: edited my question to include all of the code

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 Fattie · Jan 23, 2012 at 10:44 AM 1
Share

You're looking for COUNT. Capacity is irrelevant other than in extremely specialised situations, forget you ever heard of it.

BTW here's a handy bookmark ... http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx

2 Replies

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

Answer by Panda.du · Jan 23, 2012 at 12:18 PM

Like Fattie mentioned try Count instead of Capacity in the while-loop.

Capacity is the number of elements that the List(Of T) can store before resizing is required, while Count is the number of elements that are actually in the List(Of T).

You also have to save the changed value in your list.

 firststep[f] *= number;

or

 firststep[f] = firststep[f] * number;

Btw. i would recommend for-loop, because you initialise f and increase f by one manually.

 for(int f = 0; f < firststep.Count; f++)
 {
     firststep[f] *= number;
 }
Comment
Add comment · Show 6 · 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 cidmodder · Jan 23, 2012 at 03:37 PM 0
Share

I did this : for(int f = 0; f < firststep.Count; f++) { var number = 1+ Random.value; firststep[f] = firststep[f]* number;

     }

and got this error: Cannot implicitly convert type System.Collections.Generic.List<UnityEngine.Vector3>' to UnityEngine.Vector3[]'

I've gotten that a few other times when I've been trying this. I don't understand why it can't convert since the list is just of vector3

avatar image Panda.du · Jan 23, 2012 at 05:05 PM 1
Share

Somewhere in your code (but not in the shown snippet) you try to convert the listelement/list in a Vector3-Array (toUnityEngine.Vector3[]). But what you need is the listelement as an element of your Vector3-Array or the list as Array.

$$anonymous$$g (Wrong!) vertices = firststep[i]; or vertices = firststep;

(Right) vertices[x] = firststep[i]; or vertices = firststep.ToArray();

avatar image cidmodder · Jan 28, 2012 at 12:39 AM 0
Share

i'm not getting it. i tried doing vertices = firststep.ToArray(); and then going through vertices[f] and got the same error. the origonal vertices I get are a vector3[] and if I understand it right i just but the vertices[x] into a list.

avatar image Bunny83 · Jan 28, 2012 at 01:34 AM 0
Share

@cidmodder: Can you please check the file and the error line number of your error AND look it up in your file and tell us on which line the compiler found this error?

avatar image cidmodder · Jan 28, 2012 at 02:14 AM 0
Share

@Bunny83 this is the exact error i get: Assets/$$anonymous$$eshEditing1.cs(60,14): error CS0029: Cannot implicitly convert type System.Collections.Generic.List<UnityEngine.Vector3>' to UnityEngine.Vector3[]'

I added all the code to the question at the top as it is now and I'm going to go edit it again and bold the line its talking about

Show more comments
avatar image
1

Answer by Jaap Kreijkamp · Jan 23, 2012 at 01:43 AM

you need to use firststep.Count not Capacity, otherwise you're trying to access elements that are not created yet (the list internally uses arrays and capacity means the size of the array currently created). secondly instead of firststep[f] * number you need to write firstste[f] *= number. I don't know what your error is exactly, but you can index lists with the array operator (`[]`).

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

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

7 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

A node in a childnode? 1 Answer

Unity stops responding when i ask it to execute the same function for the third time 1 Answer

List of different classes 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