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 desiloh · May 12, 2014 at 03:22 PM · 2dspritecolorspriterendererenable

renderer.enabled doesn't work but coloring does

I have a prefab with it's Sprite renderer disabled (nothing else, no children). I want to enable in the Awake function if it meets a certain condition so I have this code:

 if(something){
 SpriteRenderer sprite= GetComponent<SpriteRenderer>();
 sprite.enabled=true;
 }

So the sprite is initially disabled and thereafter is enabled.... but it does not show the sprite! When I put that code in the Update function is always disabled, even when it is changed to enabled by code every frame.

I wanted to check if the sprite was correct so I tried this: sprite.color=Color.red; and this works and the sprite turns to red

So I tried this also: sprite.color=Color.clear; and the sprite dissapears. Do I have to use the coloring to enable/disable the sprite or does anybody knows why disable/enable is not working? Thanks in advance

Comment
Add comment · Show 2
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 desiloh · May 12, 2014 at 06:48 PM 0
Share

Is it a Unity bug? It's not possible that coloring affects the sprite but enable/disable not.

avatar image Vitor_r · May 12, 2014 at 09:04 PM 0
Share

Can u post the sprite that u are using, and maybe some screnshoot of the unity showing the problem?

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by desiloh · May 12, 2014 at 09:21 PM

I found the problem, it is in the Animator Controller, the first State has nothing and there is a transition to an animation activated by a trigger. If I delete that animation (that is not being played) everything works.

Comment
Add comment · Show 4 · 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 Pyrian · May 13, 2014 at 02:41 AM 0
Share

Oh my, I was thinking of asking whether you had an animation setting that state, but it seemed like a silly proposition.

avatar image desiloh · May 13, 2014 at 01:08 PM 0
Share

Sorry, I'm new with Unity and I thought that if I changed the state by code it would prevail over the animator, because I wasn't disabling it in that default state, it has nothing there.

avatar image Slulego · May 14, 2014 at 03:31 PM 0
Share

Ok, good. I was going to ask if you had something elsewhere that might keep changing it to "invisible."

avatar image alberto5 · Jul 09, 2016 at 01:37 AM 0
Share

Thank you! You gave me the solution. =) Same problem here...

avatar image
0
Wiki

Answer by Slulego · May 12, 2014 at 07:18 PM

In your prefab use the check button to uncheck "Sprite Renderer" and add your script to the prefab. In your script use something like:

 function Start() {
     if(true) {
         renderer.enabled = true;
     }
 }

First give that a try, then add your if statement details.

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 desiloh · May 12, 2014 at 07:45 PM 0
Share

In this moment I have a prefab with an unchecked "Sprite Renderer" in the Inspector, and a script with this:

 private SpriteRenderer sprite;

 void Awake(){
 sprite= GetComponent<SpriteRenderer>();
 }
 
 void Update(){
 gameObject.renderer.enabled=true;
 renderer.enabled=true;
 sprite.enabled=true;
 }

No 'if' statement and of course those 3 lines are not necessary, it needs just one of them, but the three of them affects the same sprite. I have two breakpoints in the first and the second line and and when it reaches the second line it is already in true. The next time it is at the first breakpoint it is in false again. I hope to be clear. Thanks

avatar image desiloh · May 12, 2014 at 07:48 PM 0
Share

$$anonymous$$y Unity version is 4.0.1

avatar image desiloh · May 12, 2014 at 08:41 PM 0
Share

I made another script and left just gameObject.renderer.enabled in the Update function, and attached it to another sprite. The result is similar but different: it shows the sprite once and then disappears again. I don't understand this behavior.

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

24 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

Related Questions

[2D] [C#] Sprite disappears after changing position 1 Answer

hide animated Sprite by code (SpriteRenderer) 2 Answers

How to access SpriteRenderer Color from a shader? 1 Answer

How to choose a size/resolution for the sprites used as game assets in Unity2D ? 2 Answers

Set Gameobject a certain color based off of other game objects 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