c# - How to modify a List<Vector3> property -
i have c# class unity has public property list called points. declared like.
public list<vector3 > points{get; set;} i want make translate method adds vector3 each element. problem having within method values updating after method returns points unchanged. think modying copy of points , not actual list.
here's have tried:
i have made new list local method called newpoints , did this.
public void translate(vector3 vect) { list<vector3> newpoints = new list <vector3>(); foreach(vector3 v in points) { newpoints. add(v +vect) ;//vect vector3 parameter } points =newpoints; } i have tried
int count = 0; foreach(vector3 v in points) { points[count] = v + vect; count++; } points =newpoints; if take pity on newbie , me out great. if there better method of accomplishing same thing know want understand why isn't working in case same issue ever comes again.
two things note:
- the list
pointsshould initialized somewhere (call constructor of list). - probably typo,
publicshould not have capital 'p'
this worked on computer:
public list<vector3> points{ get; set; } private void start() { points = new list<vector3>(); // initialize list // add dummy values test points.add(new vector3(1, 2, 3)); points.add(new vector3(4, 5, 6)); points.add(new vector3(7, 8, 9)); printpoints(); // display translate(vector3.one); // translate (1,1,1) printpoints(); // display again } public void translate(vector3 vect) { // old school loop instead of foreach can modify points directly (int = 0; < points.count; i++) { points[i] += vect; } } public void printpoints() { foreach (vector3 v in points) { debug.log(v); } } this prints:
(1.0,2.0,3.0) (4.0,5.0,6.0) (7.0,8.0,9.0) (2.0,3.0,4.0) (5.0,6.0,7.0) (8.0,9.0,10.0)
Comments
Post a Comment