unity报错【2】【CS1612】Cannot modify the return value of ‘Transform.position‘ because it is not a variabl

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// This script moves a GameObject to a new x position using Vector3.x
// Attach this script to a GameObject
// Set your x position in the Inspector

public class Vector3_x : MonoBehaviour
{
    
    
    /*Vector3 m_NewPosition;*/

    // his is the new x position.Set it in the Inspector
    public float m_XPosition;

    // Use this for initialization
    void Start()
    {
    
    
        // Initialize the vector
        /*m_NewPosition = new Vector3(0.0f, 0.0f, 0.0f);*/
        
    }

    // Update is called once per frame
    void Update()
    {
    
    
        // Press the space key to change the x position
        /*if (Input.GetKeyDown(KeyCode.Space))
        {
            m_NewPosition = m_XPosition;
        }
        // Change the position depending on vector
        transform.position = m_NewPosition;*/
        transform.position.x = m_XPosition;
    }
}

【报错】Assets\Script\Vector3_x.cs(33,9): error CS1612: Cannot modify the return value of ‘Transform.position’ because it is not a variable

transform.position.x是不能被赋值的,因为它是不是一个变量。但是transform.position可以。上述被注释的例子,通过修改一个自定义的三维变量,再赋值给transform.position。要注意的是,因为只改变x,所以自定义的三维变量的y和z要和原来一致。

参考链接
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs1612?redirectedfrom=MSDN

猜你喜欢

转载自blog.csdn.net/qq_50653422/article/details/129650129