Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by mcarthur03 · Nov 01, 2015 at 07:36 PM · scaleif

How to flip 2D object, mine isnt working nor any others i can find.

this is the program

if (Input.GetKeyDown (KeyCode.A)) { GetComponent()transform.localScale.x = -14; }

Error expecting ';' Error Unexpected sumbol '.' expecting ')',',',';','[', or '='

anyone know what I'm doing wrong?

Comment
Add comment · Show 1
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 OncaLupe · Nov 02, 2015 at 01:20 AM 0
Share

Swagathaur has the answer for what you want to do, but to clarify on why you're getting the error, this is how GetComponent would work in this situation:

GetComponent().localScale.x

Transforms are already given a quick access variable though, as Swagathaur shows just use the variable 'transform'.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Swagathaur · Nov 01, 2015 at 10:57 PM

No big deal, you are just using GetComponent() a bit wrong. If you need to use GetComponent there are a few ways to do it, but by the looks of things you shouldnt need to, simply use:

if (Input.GetKeyDown (KeyCode.A)) { transform.localScale.x = -14; }

or better yet

if (Input.GetKeyDown (KeyCode.A)) { transform.localScale.x = -transform.localScale.x; }

Comment
Add comment · Show 10 · 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 OncaLupe · Nov 02, 2015 at 01:25 AM 0
Share

The second code sample would cause the object to flip every time the user pressed A, even if they are already facing left. You need to add a direction check if you want to use that.

avatar image mcarthur03 OncaLupe · Nov 02, 2015 at 08:26 AM 0
Share

if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.A)) { transform.localScale = -7, 7, 0 } would that make them only flip If they were facing the direction they are intended to flip too?

avatar image OncaLupe mcarthur03 · Nov 02, 2015 at 04:22 PM 0
Share

The first code sample that Swagathaur posted would work. The only issue is it would be trying to set the scale even if it's already set. Not sure how much overhead that would cause since it's not actually changing every time. Personally though, I'd do something like this:

 bool facingRight = true;
 float scaleNormal = 14f;//Easier to change later if needed
 
 void Update()
 {
   if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.A) && facingRight)
   {
     facingRight = false;
     transform.localScale.x = -scaleNormal;
   }else if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.D) && !facingRight)
   {
     facingRight = true;
     transform.localScale.x = scaleNormal;
   }
 }
avatar image mcarthur03 · Nov 02, 2015 at 06:28 PM 0
Share

Swagathaur, that code seems to work. but it says I need getcomponent in a error, for some reason I have always ALWAYS needed getcomponent for things to work. know why?

avatar image OncaLupe mcarthur03 · Nov 02, 2015 at 07:27 PM 0
Share

What is the exact error you're getting? Also if you could copy/paste the lines you're using, as little details can sometimes matter.

avatar image mcarthur03 OncaLupe · Nov 02, 2015 at 09:46 PM 0
Share

will do this afternoon!

Show more comments
avatar image OncaLupe mcarthur03 · Nov 03, 2015 at 09:16 AM 0
Share

Wouldn't let me reply directly to the comment with the code, but this'll work.

I see what's wrong now. I forgot that you can't directly modify one part of the scale. This same thing happens when trying to change any multipart variable on a component, like colors, rotations, etc. Just need to do it this way:

 if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.A)) {
      Vector3 tempScale = transform.localScale;
      tempScale.x = -14;
      transform.localScale = tempScale;
 }

Sorry, should have done it this way from the start, but I keep forgetting until I see the error. :)

avatar image mcarthur03 OncaLupe · Nov 04, 2015 at 08:18 AM 0
Share

Thanks soooo much! it worked perfectly. plus I learnt a lot!

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

33 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

Related Questions

New Here: How do I resize(scale) based on set numbers [Such as by 1.5 when I drag?] 0 Answers

Scaling Crosshair 0 Answers

Stuck with scaling an object 1 Answer

UI Sliders stop working after a certain Width x Height when the canvas is set to Scale With Screen Size 0 Answers

I've been trying to write for a plane to get smaller over time in 3D, i couldn't figure it out,i've been trying to write a code for a plane to get smaller over time, i couldn't figure it out! 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