为什么我在Unity3D中收到此错误?

时间:2014-08-26 02:26:00

标签: c# unity3d

这是我得到的错误:

  

Assets / Scripts / CameraScript.cs(60,39):错误CS1612:无法修改`UnityEngine.Transform.position'的值类型返回值。考虑将值存储在临时变量

这是我的代码:

void  Start (){
        thisTransform = transform;

        // Disable screen dimming
        Screen.sleepTimeout = 0;
    }

    void  Update (){
        //Reset playerscore 
        playerScore = settings.playerScore;

        //Smooth follow player character
        if  (target.position.y > thisTransform.position.y)  {           
            thisTransform.position.y = Mathf.SmoothDamp( thisTransform.position.y, 
                                                        target.position.y, ref velocity.y, smoothTime);
        }
    }

2 个答案:

答案 0 :(得分:2)

您无法单独设置thisTransform.position.y值。改为设置thisTransform.position

例如:

if  (target.position.y > thisTransform.position.y)  {           
  thisTransform.position = new Vector3(thisTransform.position.x, 
                                       Mathf.SmoothDamp( thisTransform.position.y, target.position.y, ref velocity.y, smoothTime), 
                                       thisTransform.position.z);
}

答案 1 :(得分:2)

Transform.position.y在C#中是只读的,因此为了修改它,您需要先将Transform.position的值存储到临时变量,更改该变量的值,然后将其分配回Transform.position

Vector3 temp = thisTransform.position;
temp.y = Mathf.SmoothDamp( temp.y, target.position.y, ref velocity.y, smoothTime);
thisTransform.position = temp;
相关问题