- Home /
How to change a Vector3 list individual axis using for statement
Good evening, i have been using unity3d 2018.1 for quite a while, because of incompatibility issues with some addons i upgraded to 2018.3. Now i using a list of vector3 and changing its individual axis with a for statement but im being displayed the following error:
Assets\3DPrinting\servoArmController.cs(28,5): error CS1612: Cannot modify the return value of 'List.this[int]' because it is not a variable
Here is the abbreviated code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class servoArmController : MonoBehaviour {
public List<GameObject> servo;
public List<Vector3> servosAdjust;
// Start is called before the first frame update
void Start() {
}
// Update is called once per frame
void Update(){
baselimitRotation();
}
void baselimitRotation(){
for (int i = 0; i < servosAdjust.Count; i++){
if (servosAdjust[i].x > 360f || servosAdjust[i].x < -360f){
servosAdjust[i].x = 0f;
}
if (servosAdjust[i].y > 360 || servosAdjust[i].y < -360){
servosAdjust[i].y = 0f;
}
if (servosAdjust[i].z > 360 || servosAdjust[i].z < -360){
servosAdjust[i].z = 0f;
}
}
}
}
Answer by metalted · Jun 21, 2019 at 05:12 PM
Setting the components of a vector3 directly only works in JS not in C#. For this to work you first need to make a temporary Vector3, set all the axis and then assign it back to the list. Something like this:
void baselimitRotation(){
for (int i = 0; i < servosAdjust.Count; i++){
Vector3 temp = servosAdjust[i];
if (servosAdjust[i].x > 360f || servosAdjust[i].x < -360f){
25. temp.x = 0f;
}
if (servosAdjust[i].y > 360 || servosAdjust[i].y < -360){
temp.y = 0f;
}
30. if (servosAdjust[i].z > 360 || servosAdjust[i].z < -360){
temp.z = 0f;
}
servosAdjust[i] = temp;
}
}
This is because the list item is just a copy so changing it doesn't do anything.