Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 WintersBane · Jun 21, 2019 at 05:00 PM · scripting problem

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;
             }
         }
     }
     
     
 }
 

 
Comment
Add comment
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

1 Reply

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

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.

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

187 People are following this question.

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

Related Questions

Simplify this code 3 Answers

Rigidbody set velocity on 1 axis without changing the others. 5 Answers

Need help In creating android game with UI buttons ?Please Help 1 Answer

Scripts not updating the material's shader properties 1 Answer

Flip between 2 states constantly C#? 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