- Home /
So, what's the problem with this code?
Hey guys, I came across this script on the official unity page (http://docs.unity3d.com/ScriptReference/Renderer-material.html) and when pasted into MonoDevelop, I get this error:
Assets/example.cs(11,21): error CS0266: Cannot implicitly convert type float' to
int'. An explicit conversion exists (are you missing a cast?)
Can anyone help me solve this problem? This is the script below:
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public Material[] materials;
public float changeInterval = 0.33F;
void Update() {
if (materials.Length == 0)
return;
int index = Time.time / changeInterval;
index = index % materials.Length;
renderer.sharedMaterial = materials[index];
}
}
Well, you are trying to put a float into an integer variable, exactly where the error is telling you. (Line 11). index is an integer, Time is a float.
Answer by Landern · Aug 22, 2014 at 07:22 PM
you are declaring index as a integer, you are dividing two floats. You need to cast it to an int.
int index = (int)(Time.time / changeInterval);
Your answer
Follow this Question
Related Questions
Changing scrolling textures on a plane using transparency 1 Answer
Having a problem with the second material (on the same renderer) 0 Answers
How to script a repeating environment texture 3 Answers
Change the texture on a material at runtime without creating an instance 1 Answer
How to change material that is being scrolled for Material.SetTextureOffset 2 Answers