- Home /
Operator '/' cannot be used with a left hand side of type 'Object' and a right hand side of type 'float'.
In this code, I keep getting the error in the title, and I don't know why, given the left hand side isn't of the type Object. Here is the code:
#pragma strict
var height = 10;
var noiseSize = 15.0;
function perlinNoise(x,y){
var noise = Mathf.PerlinNoise( x / noiseSize, y / noiseSize );//error here
return noise*height;
}
Answer by robertbu · Jun 09, 2013 at 03:22 PM
Anytime you see the words 'cannot' and 'Object' in a Javascript compiler error, it is almost surly an issue of a variable declaration needing a type. Try this:
#pragma strict
var height = 10.0;
var noiseSize = 15.0;
function perlinNoise(x : float, y : float){
var noise = Mathf.PerlinNoise( x / noiseSize, y / noiseSize );//error here
return noise*height;
}
Thanks robertbu, that fixed it for me. But why does one need to declare x and y as a float? Also, how do you post replies to answers? Sorry, but I'm a noob at this.
You use the "add new comment" button on the lower right of every answer to add a reply to a question. I've converted your comment for you this time. Note that the Answer field is for specific answers to your question. You want to avoid using them for comments since it runs up the number of answer to you question and makes if far less likely that you will get a real answer.
As for the 'float'. In Javascript/Unityscript the compiler tries to deter$$anonymous$$e the type of a variable at compile time. It can get it directly if you declare it like this:
var height : float;
Or it can infer it from what you assign:
var noisSize = 15.0;
But if you declare something like this:
var height;
or in your case this:
function perlinNoise(x, y);
...the compiler has no idea if 'height' or 'x' or 'y' is a string, or a float, or an integer, or an instance of some class, or... So the compiler marks them as a base type of 'object'. Of course there is no division operation for 'object', so you get an error when you try to use it as you did in a division.