繁体   English   中英

Unity:找不到类型或名称空间名称“功能”

[英]Unity: The type or namespace name 'function' could not be found

我正在制作3D Unity游戏,我想实现一个计时器。 问题是我在脚本中收到以下错误:

无法将类型'float'隐式转换为'int'。 存在显式转换(您是否缺少演员表?)

如何解决问题?

脚本Timer.cs:

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

public class Timer : MonoBehaviour
{
    Text instruction;
    // Start is called before the first frame update
    void Start()
    {
        instruction = GetComponent<Text>();

    }

    // Update is called once per frame
    int timeLeft = 30;

    void Update()
    {
        timeLeft -= Time.deltaTime;
        instruction.text= (timeLeft).ToString();
        if (timeLeft < 0)
        {
         //   GameOver();
        }
    }
}

同样在检查器中,我似乎无法分配特定的文本字段。

编辑器视图

编辑器视图

这里:

int timeLeft = 30;
timeLeft -= Time.deltaTime;

Time.deltaTime是一个floattimeLeftint ,您需要先将float转换为int,然后再进行其余操作。

int timeLeft = 30;
timeLeft -= (int)Time.deltaTime;

这就是错误的含义

存在显式转换(您是否缺少演员表?)

是!

Time.deltaTime是float和你想的浮动分配到一个整数timeLeft -= Time.deltaTime变化timeLeftfloat

Time.deltaTime是一个浮点数。

timeLeft更改为浮动。

float timeLeft=30f;

或Cast符合预期: timeLeft-=(int)Time.deltaTime;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM