【问题标题】:Errors that I don't know how to fix我不知道如何解决的错误
【发布时间】:2014-10-31 15:14:21
【问题描述】:
#pragma strict

var targetscript : Diamond;
var yellow : Color(1,0.92,0.016,1);
var cyan : Color(0,1,1,1);
var green : Color(0,1,0,1);
var red : Color(1,0,0,1);
var magenta : Color(1,0,1,1);
var black : Color(0,0,0,1);

function Start () {
    gameObject.camera.backgroundColor = yellow;
}


function Update () {
    if (targetscript.score > 4) {
        gameObject.camera.backgroundColor = Color.Lerp(yellow, cyan);
    }

    if (targetscript.score > 9) {
        gameObject.camera.backgroundColor = Color.Lerp(cyan, green);
    }

    if (targetscript.score > 14) {
        gameObject.camera.backgroundColor = Color.Lerp(green, red);
    }

    if (targetscript.score > 19) {
        gameObject.camera.backgroundColor = Color.Lerp(red, magenta);
    }

    if (targetscript.score > 24) {
        gameObject.camera.backgroundColor = Color.Lerp(magenta);
    }

}

它给了我这些错误:

Assets/Scripts/colour.js(4,22): UCE0001: ';' expected. Insert a semicolon at the end.
Assets/Scripts/colour.js(4,22): BCE0044: expecting EOF, found '0.92'.
Assets/Scripts/colour.js(4,21): BCE0044: expecting ), found ','.
Assets/Scripts/colour.js(4,19): UCE0001: ';' expected. Insert a semicolon at the end.

我没有发现任何丢失的分号!我没觉得颜色有什么问题!为什么它给我这么多错误?我检查了脚本超过 5 次,但我没有发现任何问题!有没有人有任何想法?提前致谢

【问题讨论】:

    标签: scripting compiler-errors unity3d unityscript


    【解决方案1】:
    var xxx: yyy = zzz;
    

    这是在 Unityscript 中声明变量的方式,其中 xxx 是变量的名称,yyy 是变量的类型,zzz 是值。
    在您的情况下,您想定义一个类型,但您定义了一个值,因此出现了错误。

    将它们全部更改为:

    var yellow : Color;
    var cyan : Color;
    var green : Color;
    var red : Color;
    var magenta : Color;
    var black : Color;
    

    然后在Start()函数中,给它们赋值:

    yellow = Color(1,0.92,0.016,1);
    cyan = Color(0,1,1,1);
    green = Color(0,1,0,1);
    red = Color(1,0,0,1);
    magenta = Color(1,0,1,1);
    black = Color(0,0,0,1);
    

    【讨论】:

    • Assets/Scripts/colour.js(40,63): BCE0017: The best overload for the method 'UnityEngine.Color.Lerp(UnityEngine.Color, UnityEngine.Color, float)' is not compatible with the argument list '(UnityEngine.Color)'.
    • Color.Lerp 使用 3 个参数,如 here 所述。第一个参数是你要处理的颜色,第二个是你想要处理的颜色,第三个是处理时间。第三个参数是可选的。
    • 您得到的错误是因为您在代码中的某处仅使用了 1 个参数。可能是这个:gameObject.camera.backgroundColor = Color.Lerp(magenta);。您需要在第一个和第二个参数中指定前后颜色。
    • 对不起,它本来是(洋红色,黑色);这是一个复制和粘贴错误。但我收到Assets/Scripts/colour.js(33,63): BCE0017: The best overload for the method 'UnityEngine.Color.Lerp(UnityEngine.Color, UnityEngine.Color, float)' is not compatible with the argument list '(UnityEngine.Color, UnityEngine.Color)'. 错误
    • 尝试添加第三个参数,即 lerping 过程的时间。那应该可以。
    【解决方案2】:

    由于您正在对一个类进行变量声明,我认为每个颜色声明都需要与此类似:

    var yellow = new Color(1,0.92,0.016,1);
    

    这将创建一个具有 Color 类型的黄色变量,我们创建它的一个新实例并将其分配给黄色。

    【讨论】:

      【解决方案3】:

      你可以这样做

      #pragma strict
      var targetscript : Diamond;
      var yellow : Color = Color(1,0.92,0.016,1);
      var cyan : Color = Color(0,1,1,1);
      var green : Color = Color(0,1,0,1);
      var red : Color = Color(1,0,0,1);
      var magenta : Color = Color(1,0,1,1);
      var black : Color = Color(0,0,0,1);
      
      function Start () {
          gameObject.camera.backgroundColor = yellow;
      }
      
      
      function Update () {
          if (targetscript.score > 4) {
              gameObject.camera.backgroundColor = Color.Lerp(yellow, cyan);
          }
      
          if (targetscript.score > 9) {
              gameObject.camera.backgroundColor = Color.Lerp(cyan, green);
          }
      
          if (targetscript.score > 14) {
              gameObject.camera.backgroundColor = Color.Lerp(green, red);
          }
      
          if (targetscript.score > 19) {
              gameObject.camera.backgroundColor = Color.Lerp(red, magenta);
          }
      
          if (targetscript.score > 24) {
              gameObject.camera.backgroundColor = Color.Lerp(magenta);
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-16
        • 1970-01-01
        • 2023-01-12
        • 1970-01-01
        • 2015-01-11
        • 2022-12-07
        相关资源
        最近更新 更多