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 violence · Dec 05, 2013 at 05:11 PM · renderer.enabled

Setting renderer.enabled to true does not render gameobject

I have a list of instantiated prefabs. A function is called that loops through them and for each gameobject that is not set to owner==1, its renderer is disabled. This part WORKS. The part that does not work is when I call the function that loops back through them and re-enables the renderer using almost the exact same code, just changing false to true for the renderer setting.

Here is the loop that functions properly:

     for (var i = 0; i+1 <= landtiles.Count; i++){
         var x : Transform;
         x = landtiles[i];
         if (!(x.GetComponent(tilescript).owner == 1)){
             x.renderer.enabled = false;                
         }
     }

Here is the loop that does not:

                 for (var i = 0; i+1 <= landtiles.Count; i++){
                     var x : Transform;
                     x = landtiles[i];
                     x.renderer.enabled = true;
                     Debug.Log(x.renderer.enabled);            
                 }

I tried using the exact same code in loop 2 as in loop 1 but just changing false to true, but that did not work. So instead of limiting myself to just those gameobjects with owner == 1 I had it set renderer.enabled to true for each tile in the list. This also did not have any of the gameobjects display in game.

The kicker is Debug.Log(x.renderer.enabled) returns true. Why is this not rendering in game?

Comment
Add comment · Show 4
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 violence · Dec 05, 2013 at 06:42 PM 0
Share

Even stranger, after I execute the second loop setting the renderer back to true, debug.log shows it as set as true, but during play if I switch from Game to Scene view, the tiles that debug.log says should have a mesh renderer component enabled do not.

avatar image Kiloblargh · Dec 05, 2013 at 07:03 PM 0
Share

I have no idea, your code looks fine. Try doing it a different way anyway:

for (var t : Transform in landTiles) { var r : Renderer = t.gameObject.GetComponent.(); r.enabled = true; }

avatar image violence · Dec 05, 2013 at 07:08 PM 0
Share

Negative, this change yields nothing noticeably different. This is killing me, why would debug.log tell me the renderer is enabled but it would not be? I've got to be doing something really stupid but I can't figure out what. All searches on here and google tell me it should be working.

avatar image violence · Dec 05, 2013 at 07:44 PM 0
Share

You are absolutely right, you've gotten me close. in the script attached to each landtile, I put a debug in the update function broadcasting its renderer.enabled status and youre right, as soon as theyre turned true, they revert right back to false. I believe I have a logic error. You should convert your last comment into an answer. I'll mark it as correct as soon as I figure it out.

1 Reply

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

Answer by Kiloblargh · Dec 05, 2013 at 07:53 PM

Maybe put them all in a List. and use that instead of the List.? I think you must have another script that is doing something that you have forgotten about and turning the renderers off as soon as they are turned on. Find any code anywhere else in your scripts that disables a renderer, and put a Debug.Log message after 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 violence · Dec 05, 2013 at 08:39 PM 0
Share

Ok so I found it, I am not sure how it worked. The situation was this

  1. OnGui called gui$$anonymous$$ilitary()

  2. gui$$anonymous$$ilitary then told OnGui() to stop calling gui$$anonymous$$ilitary() and call guiRaiseArmy()

  3. guiRaiseArmy() disables renderers on tiles the player does not own to prevent him from raising an army in a tile thats not his

  4. simultaneously to guiRaiseArmy() there is guiHUD() which is a gui that runs across the top displaying some game stats. When guiRaiseArmy() is being called, guiHUD() displays a gui button that sends you back to the previous gui when its clicked AND re-enables all of the renderers.

guiRaiseArmy() was very inefficient, it was being repeatedly called by OnGui() and repeatedly setting all renderers to false. I added a boolean variable didOnce and when the renderers were disabled for the first time, it stopped. This fixed the problem. On the surface, it looked like this shouldn't have been necessary, even though its pretty bad code it still should have worked. I think there is some under the hood mechanic here that had guiRaiseArmy() get called at least once after I hit the return button guiHud(). Thats the only possible explanation I can think of, but your answer worked, I indeed was resetting the renderers to false after toggling them back to true. Thanks a lot for your help, that was a grueling few hours.

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

17 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

Related Questions

Show and hide object according to camera rotation.y position 1 Answer

2D project - player respawn delay problem 0 Answers

How do I make different gameobjects appear, every time you click the GUI.button? 2 Answers

No 'Renderer' attached to the "Main Camera" 0 Answers

renderer and prefab: "enabled" does not work? 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