- Home /
Assets/Scripts/Racing System/RacerPositionObject.cs(27,1): error CS0029: Cannot implicitly convert type `float' to `bool'
Hey Gang, Want to show my Racers Position Above the racer. I have a Plane Above Each racer with a Texture Of Its osision on it. The idea being when the posision counter (a float in anoher script) changes, the texture changers to the apropriate texture showing the Racers Posisioning Status.
using UnityEngine;
using System.Collections;
public class RacerPositionObject : MonoBehaviour {
public Texture Sixth;
public Texture Fith;
public Texture Fourth;
public Texture Third;
public Texture Second;
public Texture First;
public Renderer rend;
// Use this for initialization
void Start () {
rend = GetComponent<Renderer>();
}
// Update is called once per frame
void Update () {
// Change Hovering Object Texture Acourding to the Posision of this Racer.
if(RacingSystem.RacerPotision = 1){rend.material.mainTexture = First;}
if(RacingSystem.RacerPotision = 2){rend.material.mainTexture = Second;}
if(RacingSystem.RacerPotision = 3){rend.material.mainTexture = Third;}
if(RacingSystem.RacerPotision = 4){rend.material.mainTexture = Fourth;}
if(RacingSystem.RacerPotision = 5){rend.material.mainTexture = Fith;}
if(RacingSystem.RacerPotision = 6){rend.material.mainTexture = Sixth;}
}
}
Answer by Eno-Khaon · Jan 10, 2016 at 02:26 AM
"RacingSystem.RacerPotision"[sic] being a float is a little confusing, unless the granularity is actually factored in, but I'll simply work with it being a float in this case.
Therefore, the problem is much simpler. You're missing an '=' per if statement.
// Right now...
if(RacingSystem.RacerPotision = 1)
// It Should be...
if(RacingSystem.RacerPotision == 1)
Additionally, because the other conditions cannot be met simultaneously, each other number used (2-6) can instead be checked using:
else if(RacingSystem.RacerPotision == 6)
However, because your positions are floating point values rather than integers, it might be prudent to improve the likelihood of a match, to avoid floating point imprecision:
if(Mathf.Approximately(RacingSystem.RacerPotision, 1.0f))
Additionally, the textures themselves can be condensed down into an array to simplify the entire process further:
public Texture[] Placement; // Replaces First through Sixth
// ...
void Update()
{
rend.material.mainTexture = Placement[(int)(RacingSystem.RacerPosition - 0.5f)]; // subtracting 0.5 to preserve current functionality with integer conversion truncation.
}