Cannot convert 'UnityEngine.gameObject' to 'float'

I swiched over to unity 3.4.0 and it came up with this error:

Cannot convert ‘UnityEngine.gameObject’ to ‘float’

I don´t know how to fix this.

var waterLevel : float;
var uAudio : AudioClip;
var aAudio : AudioClip;

var uColor = Color(1,1,1,1);
var uDensity = .05;

var aColor = Color(1,1,1,1);
var aDensity = .008;

var waterSurface : Renderer;
var underwaterSurface : Renderer;

private var below = false;
private var glow : GlowEffect;
private var blur : BlurEffect;


function Awake() {
	if(!waterLevel)
	{
		water = FindObjectOfType(Water);
[COLOR="Blue"]                if(water) waterLevel = water.gameObject;[/COLOR]
	}
	aColor = RenderSettings.fogColor;
	aDensity = RenderSettings.fogDensity;
	
	glow = GetComponent(GlowEffect);
	blur = GetComponent(BlurEffect);
	if( !glow || !blur )
	{
		//Debug.LogError("no right Glow/Blur assigned to camera!");
		enabled = false;
	}
	if( !waterSurface || !underwaterSurface )
	{
		//Debug.LogError("assign water  underwater surfaces");
		enabled = false;
	}
	if( underwaterSurface != null )
		underwaterSurface.enabled = false; // initially underwater is disabled
}

function Update ()
{
	if (waterLevel < transform.position.y  below)
	{
		audio.clip = aAudio;
		audio.Play();
		RenderSettings.fogDensity = aDensity;
		RenderSettings.fogColor = aColor;
		
		below = false;
		
		glow.enabled = !below; 
		blur.enabled = below; 
		waterSurface.enabled = true;
		underwaterSurface.enabled = false;
	}
	
	if (waterLevel > transform.position.y  !below)
	{
		audio.clip = uAudio;
		audio.Play();
		RenderSettings.fogDensity = uDensity;
		RenderSettings.fogColor = uColor;
		
		below = true;
		
		glow.enabled = !below; 
		blur.enabled = below;
		waterSurface.enabled = false;
		underwaterSurface.enabled = false;
	}
}

waterLevel is a float
water.gameObject is a GameObject

They don’t match.

if(water) waterLevel = water.gameObject.transform.position.y;

might be what you’re after.

-edit-

fixed code (thanks Afisicos)

1 Like

you can’t asing a gameObject to a float variable. waterLevel is a float.

yes. tertle said the correct code, but is water.gameObject.transform.position.y

Thanks. But this is weird i opened the old unity and the new one again, and the error disappeared

I actually got this error too when moving to 3.4 only IOS build (not on standalone).

Haven’t figure how to get rid of it yet!

In the end i figured my issue was I took the shortcut

var myNumber = 0.0; as a cheat in javascript

but when i changed it to var myNumber : float = 0.0; it didn’t have the issue. Seemed to be picking it up as an object because I didn’t explicitly declare it.

look at this

this set waterLevel to a float variable now look what your trying to assign to it

you can see it does not match what you want to do is this

so whuddanaim got it right…

I just want to point out that not the OP made that error in the code, but whoever wrote the “IslandDemo” or whoever wrote the 3.4 version of Unity. It is weird to see official demos not working anymore with new versions of the program.

Sadly, that is not really true with Unity. And I have also posted a wishlist item for this. Internally Unity has an implicit bool cast for UnityEngine.Object and whatsoever. Since I am not familiar with Unity Script I only know from real JavaScript that converting bool into float should be possible. So I would say that this type conversion could have worked in Unity 3.3 by doing Object->Bool->Float…
But since 3.4 they made typing stricter in US, so that this will fail now, I suppose…

BTW, this is terrible coding practice anyway, so its at least some sort of improvement that this fails now!

Hi,

You can use the following code instead of code line 23.// if(water) waterLevel = water.gameObject;

if (water) waterLevel = water.transform.position.y;

Thanks
Ram