- Home /
Operator '+' cannot be used with a left hand side of type 'Object' and a right hand side of type 'float'
I'm trying to create an infinite runner and am seeing if I can translate some of the C# codes into Javascript. I've currently run into a problem that I can't really seem to fix. I get that error for this line of code (also listed below):
//This way gets the point at which you should add the room, so that it started straight after the last room.
// Operator '+' cannot be used with a left hand side of type 'Object' and a right hand side of type 'float'.
var roomCenter: float = farhtestRoomEndX + roomWidth * 0.5f;
I don't understand why that is? Is it how I'm declaring the variable farhtestRoomEndX? (it was already declared as a float in the line just above it).
For reference, this is the c# line: float roomCenter = farhtestRoomEndX + roomWidth * 0.5f;
And the whole code I've written so far
#pragma strict
var availableRooms: GameObject[]; //this will contain an array of prefabs which the script will generate
var currentRooms: GameObject[]; //this will check our current room, when it should create new rooms & when to delete old rooms
private var screenWidthInPoints: float; //will cache the screen size in points to determine next rooms & delete old rooms
var farhtestRoomEndX: float;
function Start()
{
//calculate the size of the screen in points
var height: float = 2.0f * Camera.main.orthographicSize;
screenWidthInPoints = height * Camera.main.aspect;
}
function AddRoom(farhtestRoomEndX)
{
//Picks a random index of the room type (Prefab) to generate.
var randomRoomIndex: int = Random.Range(0, availableRooms.Length);
//Creates a room object from the array of available rooms using the random index above.
var room: GameObject;
room = Instantiate(availableRooms[randomRoomIndex]);
//Get the size of the floor inside the room, which is equal to the room’s width.
var roomWidth: float = room.transform.FindChild("floor").localScale.x;
//This way gets the point at which you should add the room, so that it started straight after the last room.
// Operator '+' cannot be used with a left hand side of type 'Object' and a right hand side of type 'float'.
var roomCenter: float = farhtestRoomEndX + roomWidth * 0.5f;
//This sets the position of the room. You need to change only the x-coordinate since all rooms have the same y and z coordinates equal to zero.
room.transform.position = new Vector3(roomCenter, 0, 0);
//add the room to the list of current rooms.
currentRooms.Add(room);
Answer by robertbu · Nov 16, 2014 at 11:50 PM
Your code is confusing. You declare:
var farhtestRoomEndX: float;
...at the top of the file, but you use the same name for the parameter here:
function AddRoom(farhtestRoomEndX)
You fix the immediate problem by change the function declaration to:
function AddRoom(farhtestRoomEndX : float)
...but you might want to change the name of the parameter, or don't use a parameter if you want to use the variable.
Also built-in arrays don't have an Add() function. Consider using a generic List.
Thank you! That method work though I do not fully understand why I have to declare it as a float twice if it's the same var?
As for the add function I am quite new to arrays, with built in arrays I can't resize it which means I can't add to it. I have to recreate the array to add to it yes? Is there any example code that I could follow? Unity's API isn't helping me much here.
I do not fully understand why I have to declare it as a float twice if it's the same var?
That's the whole point. Though it has the same name, it is not the same variable. I suggest you do one of two things. Either:
function AddRoom()
Then it will use the variable at the class level, or you can do:
function AddRoom(somethingElse : float)
Which makes it clear that you are using the passed variable (the code would need to change as well to use the new parameter).
As for arrays, this is a good place to start. I repeat, a generic List is a good collection to use if you want to add items.
A few random thoughts
You aren't declaring it twice. In your function you declare it as a local variable, this version is only accessible inside your function. You have declared a separate variable at the class (script) level. This is going to get you into trouble later.
If you want to resize an array at run time you are better off using a generic List. There is a pretty decent tutorial in the learn section on scripting for generic collections.
Note that in general JavaScript is not very friendly when it comes to typing. The var keyword lets you get into a lot of trouble.
Answer by DedValve · Nov 17, 2014 at 04:37 AM
I think I solved it! Rather than keeping the built in array I made it into a (normal?) javascript array
ar currentRooms = new ArrayList();
and kept the same line of code of the add() function.
NO, NO, NO.
You do not solve things by building an ArrayList. You've simply set yourself up for bigger headaches and slow performance later. Follow one of the links in $$anonymous$$e or @robertbu's comments to learn more about generic Lists.
Your answer
Follow this Question
Related Questions
Operator '+' cannot be used 1 Answer
Error CS0023: The '!' operator cannot be applied to operand of type 'void' 1 Answer
C# Error help ( error CS0023 The `!’ operator cannot be applied to operand of type `string’ ) ??? 1 Answer
error CS0019: Operator `>' cannot be applied to operands of type `UnityEngine.Vector3' and `int' 0 Answers