• 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 /
  • Help Room /
This question was closed Dec 09, 2016 at 11:55 AM by nj4242 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by nj4242 · Dec 09, 2016 at 09:46 AM · errorgameobjectvector3transform.positionbounds

Operator `+' cannot be applied to operands of type `UnityEngine.Vector3' and `float'

Simple but out of my head, (Much noob)

What's with this error, I know what it means, but I don't know where I am wrong:

 spriteBound =  gameObject.GetComponent<SpriteRenderer>().sprite.bounds;
 
 vector3 upperLeft = transform.position - transform.right * spriteBound.extents.x + transform.up + spriteBound.extents.y;

Thanks

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

3 Replies

  • Sort: 
avatar image
2
Best Answer

Answer by 5c4r3cr0w · Dec 09, 2016 at 09:55 AM

Error message is simply suggesting that you are trying to add Vector3 or and float.

'+' operator can only be applied if there are similar data types at both sides of it. Here transform.position and transform.up are vector3. and spriteBound.extents.x and spriteBound.extents.y are float values.

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 AlexTuduran · Jan 17, 2019 at 11:55 AM 0
Share

Although it would probably not make much functional sense for most of us, the add operation between a Vector3 and a float could be defined as a new Vector3(v.x + f, v.Y + f, v.z + f) much in the same way multiplication with a float is new Vector3(v.x f, v.y f, v.z * f).

avatar image
1

Answer by KazYamof · Dec 09, 2016 at 09:57 AM

That means you can't sum a Vector with a constant value, it makes no sense. You can scale a vector by a constant, so a Vector3.one * 5 = Vector3(5,5,5). I don't know what are you trying to achieve, but fix your operation:

This way you scale the transform.right by the sum of x + up + y..

 Vector3 upperLeft = transform.position - transform.right * (spriteBound.extents.x + transform.up + spriteBound.extents.y);

This is the syntax, but the semanthics is up to you.

Comment
Add comment · 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
2

Answer by RobAnthem · Dec 09, 2016 at 10:50 AM

You can't apply single vector changes to a whole vector3, you have to make it 3 seperate equations if you want to define X, Y, and Z, seperately, even if you define Vector3 as a new vector, you'll need to define the X, Y, and Z individually in the constructor.

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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

98 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Array index out of range(C#) 1 Answer

Framing multiple objects from a list along y axis only? 0 Answers

Gameobjects transform position minus range 1 Answer

NullPointException Object not found, caused by a variable not accepting value? 0 Answers

Rotat a GameObject and than shoot it in the New (x) direction 1 Answer


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