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 Apr 25, 2017 at 04:23 AM by x20X-Studios834 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by x20X-Studios834 · Apr 20, 2017 at 10:02 PM · 2d2d game

Enabling Sprite Renderer in C#

Hi! I'm looking for the simplest way to turn on/off a sprite renderer in C#, I have looked at tutorials and unfortunately found myself with more questions than answers. All I need is for a button to be pressed, and have a sprite appear, I tried deactivating the whole object but that obviously stopped it from turning back on. Any help is greatly appreciated, thank you.

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
5
Best Answer

Answer by Commoble · Apr 20, 2017 at 10:04 PM

Firstly, your script needs a reference to a sprite renderer. The first step to doing that is to declare this field in your script:

 public SpriteRenderer spriteRenderer;

To assign the SpriteRenderer component to this field, you can either link the renderer component to your script in the Inspector (details here and here), or you can use GetComponent when the script initializes:

 void Start()
 {
     this.spriteRenderer = GetComponent<SpriteRenderer>();
 }

Now that you have a reference to the SpriteRenderer component, whenever you need to turn the renderer on or off, you can use spriteRenderer.enabled to set whether it's enabled or disabled:

 this.spriteRenderer.enabled = true; // enable the renderer
 this.spriteRenderer.enabled = false; // disable the renderer


Comment
Add comment · Show 2 · 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 Commoble · Apr 20, 2017 at 10:09 PM 1
Share

Side note: This answer assumes your script and spriterenderer are on the same object. If they're on separate objects, GetComponent won't work (unless you have a reference to the other object, in which case you call otherGameObject.GetComponent<SpriteRenderer>().

If both objects are in the scene when the scene starts, linking objects via the inspector still works here; you can declare your public SpriteRenderer spriteRenderer; field and drag the other gameobject to that field, and it'll assign the SpriteRenderer component on that other object to that field.

avatar image x20X-Studios834 Commoble · Apr 21, 2017 at 08:26 AM 0
Share

Thanks Commoble, this worked perfectly :)

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

The player randomly freezes in place while other objects move ingame 1 Answer

,Hey! I'm trying to make a Dash move like Celeste but my Dash doesn't seem to work Horizontally(X Axis), My Dash is working perfectly fine Upward and Downwards(Y Axis). 1 Answer

Level design clone of King of Thieves 0 Answers

Horizontal attraction? 0 Answers

trail renderer shows above everything (Unity 2D sorting layer ) 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