簡體   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