C# 统一:类型或名称空间名称';功能';找不到
我正在制作一个3D Unity游戏,我想实现一个计时器。问题是我在scirpt中收到以下错误: 无法将类型“float”隐式转换为“int”。明确的 存在转换(您是否缺少演员阵容?) 如何解决这个问题 脚本计时器。cs:C# 统一:类型或名称空间名称';功能';找不到,c#,unity3d,C#,Unity3d,我正在制作一个3D Unity游戏,我想实现一个计时器。问题是我在scirpt中收到以下错误: 无法将类型“float”隐式转换为“int”。明确的 存在转换(您是否缺少演员阵容?) 如何解决这个问题 脚本计时器。cs: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Timer : MonoBehaviour {
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();
}
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
公共类计时器:单行为
{
文本教学;
//在第一帧更新之前调用Start
void Start()
{
指令=GetComponent();
}
//每帧调用一次更新
int timeLeft=30;
无效更新()
{
timeLeft-=Time.deltaTime;
指令.text=(timeLeft.ToString();
如果(时间间隔<0)
{
//GameOver();
}
}
}
此外,在inspector中,我似乎无法指定特定的文本字段
时间。deltaTime
是一个浮点数,您试图在timeLeft-=Time中为一个整数分配一个浮点数。deltaTime
将timeLeft
更改为float时间。deltaTime
是一个浮点数
将timeLeft
更改为浮动
float timeLeft=30f;
或者按照它的预期:
timeLeft-=(int)Time.deltaTime代码>此处:
int timeLeft = 30;
timeLeft -= Time.deltaTime;
Time.deltaTime
是一个float
和timeLeft
是int
,在执行其余操作之前,需要将float转换为int
int timeLeft = 30;
timeLeft -= (int)Time.deltaTime;
这就是错误的意思
存在显式转换(是否缺少强制转换?)
对! 你是从哪里得到这个错误的?你应该首先问谷歌这个错误是什么意思。