- Home /
How am I getting this error??
The error is;
" Operator '+' cannot be used with a left hand side of type 'float' and a right hand side of type 'Object'."
the code in question is;
#pragma strict
var coolTimeMax: float = 8.0f;
var isUseSkill: boolean = false;
var buttonClickTrue: AudioClip;
var buttonClickFalse: AudioClip;
private var timer: float = 1.0f;
private var coolTime: float = 0.0f;
private var silder: UISlider = null;
private var getCam: GameObject;
private var soundRate: float = 0.0; // variable to hold sound rate
private var soundDelay: float = 0.0;
function Start ()
{
silder = GetComponent(UISlider);
getCam = GameObject.FindGameObjectWithTag("MainCamera");
}
function Update ()
{
silder.sliderValue = timer;
if (UICamera.hoveredObject != null && UICamera.hoveredObject.name == ("btn_Farmer") && Input.GetMouseButton(0) && !isUseSkill && timer == 1)
{
PlaySound ( buttonClickTrue, 0 );
getCam.GetComponent(InGameGUI).SetBuildChoice ( gameObject );
}
if (isUseSkill)
{
StartTimer ();
isUseSkill = false;
}
}
function StartTimer ()
{
var t: float = 0.0f;
while (t < 1)
{
t += Time.deltaTime / coolTimeMax;
timer = Mathf.Lerp (0, 1, t);
yield;
}
}
function PlaySound ( soundName, soundDelay ) // calls to play sound based on sound file name attached to object
{
if ( !audio.isPlaying && Time.time > soundRate ) // sets sound rate
{
soundRate = Time.time + soundDelay; // option to add a delay to the sound before it starts playing
audio.clip = soundName;
audio.Play ();
yield WaitForSeconds ( audio.clip.length );
}
}
the exact line this error is pointing too is;
soundRate = Time.time + soundDelay;
This makes no sense! last I soundRate is a float, Time.time is a float, and soundDelay is a float! Where does it get this "Object" type from?!
Answer by Eric5h5 · Oct 04, 2013 at 02:43 PM
It makes complete sense since you haven't defined the types for the variables in your PlaySound function. You have two soundDelay variables; a global one and a local one in the PlaySound function.
Was about to post an answer, but then saw Eric already posted.
You need to change
function PlaySound ( soundName, soundDelay )
to
function PlaySound ( soundName, soundDelay : float )
soundDelay in the function declaration is not the same as soundDelay declared in a script, so compiler doesn't know what's its type. You have to specify it.
I'm afraid I don't follow you Eric. I've used this function several times and have never gotten an error like this.
@ArkaneX: almost...you need to define the types of all variables.
function PlaySound ( soundName : AudioClip, soundDelay : float )
Ok thanks for the clear up guys, This is weird because I've used this function as is several times before and never have I had to declare the type inside the Function command. But what you said is true, Ark, once I typed in float, the error went away.
Thanks!
@Eric5h5 - it's not necessary, because without specifying AudioClip for soundName it works properly, although with implicit downcast warning.
But generally I agree with you - soundName (or any other parameters) should be declared explicitly to prevent warnings.