[英]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
是一個float
而timeLeft
是int
,您需要先將float轉換為int,然后再進行其余操作。
int timeLeft = 30;
timeLeft -= (int)Time.deltaTime;
這就是錯誤的含義
存在顯式轉換(您是否缺少演員表?)
是!
Time.deltaTime
是float和你想的浮動分配到一個整數timeLeft -= Time.deltaTime
變化timeLeft
到float
。
Time.deltaTime
是一個浮點數。
將timeLeft
更改為浮動。
float timeLeft=30f;
或Cast符合預期: timeLeft-=(int)Time.deltaTime;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.