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 Darkhydro · Mar 31, 2012 at 01:23 AM · gameobjectrendererresetvisible

How can I control GameObject visibility?

I've searched around on the forums and online, and this would seem to be a simple question from what I found. The best method I found for controlling visibility of GameObjects was setting

myObject.renderer.enabled = false; //makes object invisible myObject.renderer.enabled = true; //makes object visible

This method actually works most of the time for me. However, in my game, I use a Reset method (that I created), which is called by a BroadcastMessage sent when the player dies. This Reset method is supposed to reset all objects in the level. It is getting called, but the contents of it are not working. It is important to note that the GameObject I am using has an attached animation as well, which I attempt to Rewind in this method as well to no avail. Here is the specific code for the Collectable class I have:

 protected bool Reset()
 {
     animObject.renderer.enabled = true;
     if (animObject && animObject.animation.GetClip("collect"))
             animObject.animation.Rewind("collect");
     if (this.animation && this.animation.GetClip("collect"))
         this.animation.Rewind("collect");
     
     return true;
 }

Using Debug statements I ascertained that the method IS getting called, and the renderer.enabled variable is getting set to true. However, when I pause the game, the mesh renderer is disabled in the editor! And regardless of whether I pause or not, the GameObject I was trying to set visible again never shows up, but IS in the right spot and is collidable.

It perplexed me further when I found that setting the renderer.enabled variable worked just fine in other methods, but whenever I called it from this Reset method it didn't work. None of the other GameObjects being reset with the BroadcastMessage affect these collectables that should be getting reset.

Am I doing something wildly wrong?

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

2 Replies

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

Answer by rutter · Apr 03, 2012 at 06:09 PM

I could be missing it, but I don't think the problem is in the particular bit of code you've posted. With what little I know, here, the first few things I'd check are:

  • Are you calling BroadcastMessage() on every object you're trying to reset? The function only operates on one object and its children.

  • If you are iterating objects to call BroadcastMessage() in a loop, it's important to remember that many of Unity's "find" functions won't necessarily return disabled objects. (Have you called SetActiveRecursively(false) on anything?)

  • Is it possible that some other function(s) are messing with your renderers after the reset call?

This may be a good chance to practice using MonoDevelop's built-in debugger. You can attach it to your Unity process, add a breakpoint around the time of the reset call, and watch your code go line-by-line. It's time consuming, but it'll help you understand exactly what your code is doing.

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 Darkhydro · Apr 03, 2012 at 06:48 PM 1
Share

It turns out someone else's code was messing with the mesh after I Reset the object. It was being flagged for destruction again even after Reset, so was going invisible again immediately. Sorry for making this seem like a complex question, it just took me a while to find the error.

avatar image
0

Answer by garner · Mar 31, 2012 at 01:18 PM

You never set
animObject.renderer.enabled = false;

so it'll never hide. If it's disabled in the editor it's because you need have the gameobject unchecked when the play button is off because there's no code here to disable it.

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 Darkhydro · Apr 03, 2012 at 05:23 PM 0
Share

I'm sorry, I thought I made it clear in the question that I used exactly that statement and it worked just fine. Just to be clear, I do set it to false, and it does work. Getting it to re-enable is my problem, and only during this Reset method.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Hide gameobject until certain condition has been met,Hide an object until a condition is met. 2 Answers

Rendermode: Cutout not working on gameobject from external asset bundle 2 Answers

Help! Skinned mesh render not visible!! 3 Answers

Unity3D C# enable renderer for a few seconds 1 Answer

Material class use general question 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