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);