- Home /
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
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
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;
}
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
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();
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.
@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?
@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
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 (`[]`).
Your answer
Follow this Question
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