- Home /
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?
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.
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; }
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.
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.
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.
Ok so I found it, I am not sure how it worked. The situation was this
OnGui called gui$$anonymous$$ilitary()
gui$$anonymous$$ilitary then told OnGui() to stop calling gui$$anonymous$$ilitary() and call guiRaiseArmy()
guiRaiseArmy() disables renderers on tiles the player does not own to prevent him from raising an army in a tile thats not his
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.