我只是在Unity游戏引擎中使用c#编码,我一直遇到这个错误:
Assets / Background / SkyManager.cs(30,29):错误CS0266:不能 隐式转换类型
float' to
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);
}
}
答案 0 :(得分:6)
在您的第int sunRotation = 7.5f * hours;
行上,您使用的是7.5f
,这是一个浮点值。您正尝试将其放在int
变量中。
您可以使用以下内容将数据转换为int
。
int sunRotation = (int)(7.5f * hours);
答案 1 :(得分:2)
这里的代码是一个返回浮点数:
echo
但是你试图将它指定为整数。您可以将该值转换为Juken建议的整数,但是您将丢失您可能希望它维护的任何小数值。
您应该考虑使用Math.Ceil或Math.Floor根据您的意图向上或向下舍入您的值,或者将值分配给浮点数以保持准确性。