首页 > 解决方案 > 为什么 Unity 不更新脚本?

问题描述

我正在使用 Unity 2018.4.0f1 进行 Unity 入门课程。

出于某种原因,Unity 没有更新我的脚本,即使在 Visual Studio 上保存并在我的 Unity 脚本文件夹中正确显示之后也是如此。

这是我的 Unity 程序片段,它正在正确显示。

在此处输入图像描述

但是,每当我进入播放模式时,属性都会重置为我在预更新脚本中的值。

在此处输入图像描述

我的程序很简单,因此我确定这不是软件问题。我还发现这里提出了类似的问题:

https://forum.unity.com/threads/why-is-unity-not-updating-my-scripts.497383/

以及这里:

https://answers.unity.com/questions/17460/unity-is-not-updating-my-scripts.html

这些都不包含一个决定性的答案,我无法解决这个问题。

我的完整程序如下:

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

public class PlayerController : MonoBehaviour
{
    public float speed = 10.0f;
    public float turn = 25.0f;
    public float horizontalInput;
    public float verticalInput;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        horizontalInput = Input.GetAxis("Horizontal");
        verticalInput = Input.GetAxis("Vertical");

        // We'll move the player forward
        transform.Translate(Vector3.forward * Time.deltaTime * speed * verticalInput);
        transform.Rotate(Vector3.up, Time.deltaTime * turn * horizontalInput);
    }
}

标签: c#unity3d

解决方案


当您有也被序列化的公共变量时,来自编辑器的值会覆盖脚本中声明的值,您可能必须删除场景的一些元数据才能完全重置它。只需从编辑器更改它,或将这些值设为私有,然后编辑器将无法访问它们并且不会更改值


推荐阅读