简体   繁体   English

错误 CS0266:无法将类型“float”隐式转换为“int”。 存在显式转换(您是否缺少演员表?)

[英]error CS0266: Cannot implicitly convert type 'float' to 'int'. An explicit conversion exists (are you missing a cast?)

Im trying to make some text display a price that changes by 5 present every time you press a button.我试图让一些文本显示一个价格,每次按下按钮时,价格都会改变 5。 Sorry if i explain badly im new to stack and coding.抱歉,如果我对堆栈和编码新手解释得很糟糕。 Just ask me if you need more context.只要问我您是否需要更多上下文。 This is unity using visual studio code c#.这是使用 Visual Studio 代码 c# 的统一。

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

public class clicksUpPerSec : MonoBehaviour
{
  public Button addBut1PS;
  public GameObject ShowClickerPrice;
  public static int perSec = 0;
  public static float ClickerPrice = 10f;
  public float Prosent = 5f;

  void Start()
  {
      Button btn = addBut1PS.GetComponent<Button>();
  btn.onClick.AddListener(TaskOnClick);

  }


  void Update() {
      
      ShowClickerPrice.GetComponent<Text>().text = "COST: " + ClickerPrice;
  }

  void TaskOnClick(){

      if(Click.clicks >= ClickerPrice) {
          perSec = perSec + 1;
          Click.clicks = Click.clicks - ClickerPrice;
          ClickerPrice = (((Prosent  * ClickerPrice) / 100f) + ClickerPrice);
      }
      
  }
}
    Click.clicks = Click.clicks - ClickerPrice;

Here you are trying to assing float value to an integer field and there is no implicit conversion for that, you need to cast (int) somewhere to convert value expilictly:在这里,您尝试将浮点值分配给整数字段,并且没有隐式转换,您需要在某处强制转换 (int) 以显式转换值:

Clicks.clicks = Click.clicks - (int)ClickerPrice;

or或者

Click.clicks = (int)(Click.clicks - ClickerPrice);

暂无
暂无

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

相关问题 错误:CS0266无法将类型&#39;int&#39;隐式转换为&#39;byte&#39;。 存在显式转换(您是否缺少演员表?) - Error: CS0266 Cannot implicitly convert type 'int' to 'byte'. An explicit conversion exists (are you missing a cast?) 错误 CS0266 无法将类型“Newtonsoft.Json.Linq.JObject”隐式转换为“字符串”。 存在显式转换(您是否缺少演员表?) - Error CS0266 Cannot implicitly convert type 'Newtonsoft.Json.Linq.JObject' to 'string'. An explicit conversion exists (are you missing a cast?) CS0266 C# 无法将类型“UnityEngine.Object”隐式转换为“”。 存在显式转换(您是否缺少演员表?) - CS0266 C# Cannot implicitly convert type 'UnityEngine.Object' to ''. An explicit conversion exists (are you missing a cast?) 错误CS0266:无法将类型&#39;object&#39;隐式转换为&#39;int&#39; - error CS0266: Cannot implicitly convert type 'object' to 'int' 无法将浮点类型隐式转换为 int。 存在显式转换(您是否缺少演员表?) - Cannot implicitly convert type float to int. An explicit conversion exists (are you missing a cast?) 错误1无法将类型&#39;int&#39;隐式转换为&#39;uint&#39;。 存在显式转换(您是否错过了演员?) - Error 1 Cannot implicitly convert type 'int' to 'uint'. An explicit conversion exists (are you missing a cast?) 条件运算符。 错误CS0266无法将类型&#39;int&#39;隐式转换为&#39;byte&#39; - Conditional operator. Error CS0266 Cannot implicitly convert type 'int' to 'byte' 无法隐式转换类型,存在显式转换(您是否缺少强制转换?) - Cannot implicitly convert type, an explicit conversion exists (are you missing a cast?) 无法隐式转换类型,存在显式转换(您是否缺少强制转换?) - Cannot implicitly convert type, explicit conversion exists (are you missing a cast?) 无法将类型&#39;object&#39;隐式转换为&#39;int&#39;。 存在显式转换(您是否缺少演员表?) - Cannot implicitly convert type 'object' to 'int'. An explicit conversion exists (are you missing a cast?)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM