Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DedValve · Nov 16, 2014 at 11:48 PM · erroroperatorinfinite runner

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DedValve · Nov 17, 2014 at 04:20 AM 0
Share

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.

avatar image robertbu · Nov 17, 2014 at 04:24 AM 1
Share

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.

http://wiki.unity3d.com/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use%3F

avatar image Kiwasi · Nov 17, 2014 at 04:27 AM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kiwasi · Nov 17, 2014 at 05:01 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Help with this error: Operator '*' cannot be used with a left hand side of type 'Object' and a right hand side of type 'UnityEngine.Vector3'. 1 Answer

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges