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 /
This question was closed Feb 15, 2018 at 07:12 PM by poppysilvers1997 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by poppysilvers1997 · Feb 15, 2018 at 03:54 AM · spriteflippinglookatmouse

Sprite Flip Towards Camera

Concept is simple, character turns around based on which side of the screen the mouse is on. The script is:

void Update () { var MousePosition = Input.mousePosition; var PlayerPosition = Camera.main.WorldToScreenPoint(transform.position); if (MousePosition.x < PlayerPosition.x) { SpriteRenderer.flipX = true; }

     if (MousePosition.x > PlayerPosition.x)
     {
         SpriteRenderer.flipX = false;
     }
 }

I'm getting the error "CS0120: An object reference is required to access non-static member 'UnityEngine.SpriteRender.flipX"

I don't know exactly what I've done wrong. If sprite render flip is a static variable, how would I edit it? Or is something else a static variable? I'm not sure really, var is still somewhat confusing to me.

Thanks for any help!

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

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by ElijahShadbolt · Feb 15, 2018 at 06:36 AM

Note that SpriteRenderer.flipX is not static, which means you need to call flipX from an instance of the SpriteRenderer class. Your GameObject has a SpriteRenderer component attached (which is an instance of the class SpriteRenderer, not the static class itself). You could use the following code to fix the error.

Replace

 SpriteRenderer.flipX = false;

with

 SpriteRenderer sr = this.GetComponent<SpriteRenderer>();
 sr.flipX = false;

See also:

  • GetComponent()

  • NullReferenceException

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 poppysilvers1997 · Feb 15, 2018 at 07:12 PM 1
Share

Thanks! I actually managed to narrow that down to just { this.GetComponent().flipX = false; } Thanks for the assistance! I still had a narrow understanding of the "this" and "getcomponent" commands but I think I understand them now. Thank you!

Follow this Question

Answers Answers and Comments

102 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 avatar image avatar image avatar image avatar image

Related Questions

Flipping a group of sprites 1 Answer

flip sprite while using mathf.sin 0 Answers

Angles invert after flipping player by 180 on Y-axis in 2D. 0 Answers

Flip sprites in enemy spawner 1 Answer

Teleport wrapping effect/Mask 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