C# 为什么我总是得到一个隐式转换错误?
我只是在Unity游戏引擎中使用c编写代码,我一直遇到以下错误: Assets/Background/SkyManager.cs30,29:错误CS0266:无法 隐式转换类型float'toint'。显式转换 你缺演员吗 这是我的密码:C# 为什么我总是得到一个隐式转换错误?,c#,C#,我只是在Unity游戏引擎中使用c编写代码,我一直遇到以下错误: Assets/Background/SkyManager.cs30,29:错误CS0266:无法 隐式转换类型float'toint'。显式转换 你缺演员吗 这是我的密码: using UnityEngine; using System; using System.Collections; public class SkyManager : MonoBehaviour { public int
using UnityEngine;
using System;
using System.Collections;
public class SkyManager : MonoBehaviour {
public int hours = DateTime.Now.TimeOfDay.Hours;
public int minutes = DateTime.Now.TimeOfDay.Minutes;
public int seconds = DateTime.Now.TimeOfDay.Seconds;
public Light lighting;
//public CameraClearFlags night;
public Camera night;
// Use this for initialization
void Start () {
print (DateTime.Now.TimeOfDay.TotalSeconds);
}
// Update is called once per frame
void Update () {
//what should the rotation of light be?
//15° every hour
if(hours != 1){
int sunRotation = 7.5f * hours;
print (sunRotation);
}
//end
var rot = transform.rotation;
lighting.transform.rotation = rot * Quaternion.Euler(180, 0, 0);
}
}
在您的线路上,int SUNROTION=7.5f*小时;,您使用的是7.5f,它是一个浮点值。您正在尝试将其放置在int变量中
您可以使用以下命令将数据转换为int
这里的代码是一个返回浮点值的代码: int太阳旋转=7.5f*小时 但您正在尝试将其指定为整数。您可以按照Juken的建议将该值强制转换为整数,但是您可能会丢失希望它保持的任何十进制值 <>你应该考虑使用Maul.CeIL或Madio.Load来根据你的意图来决定你的价值是上升的还是下降的,或者也许把这个值分配给一个浮标来保持精度。< /P>
int sunRotation = (int)(7.5f * hours);