- Home /
renderer and prefab: "enabled" does not work?
Hi,
would you know why this code does not work? Here, "instance" instantiates the prefab, and i would like to hide it, and show it only when i click on the right arrow. But instead it appears at the beginning, would you know how to achieve this?
I added a plane, a light and a character in an empty, and i added a "mesh renderer" to this empty, then i put all of them in a prefab in the "Resources" folder :
Thanks
SOLUTION:
var instance: GameObject;
private var allRenderers : Component[];
function Start () {
instance = Instantiate(Resources.Load("totalR", GameObject));
instance.renderer.enabled = false;
allRenderers = instance.GetComponentsInChildren(Renderer);
for (var aRenderer : Renderer in allRenderers) {
aRenderer.enabled = false;
}
}
function Update () {
if (Input.GetAxis("Horizontal")>0){
instance.renderer.enabled = true;
allRenderers = instance.GetComponentsInChildren(Renderer);
for (var aRenderer : Renderer in allRenderers) {
aRenderer.enabled = true;
}
}
}
I'm guessing you have multiple renderers in that prefab, you'd need to turn them all off...
@whydoidoit thanks! you were right, i edited my post ;) if i put all the code in the Update function, and want to know when the prefab has been loaded, can i just add a boolean right after the loop, and check when the boolean is "on" before checking the Input GetAxis ? Is there a better way?
You don't need the instance.renderer.enabled = true as GetComponentsInChildren also returns the renderer on the actual object (confusingly!)
@whydoidoit alright thanks, any idea about my second question? thanks anyway for your help
So it's just appearing straight away still? Hmm. Perhaps a check for the actual key press? Using Input.Get$$anonymous$$eyDown(...)
Answer by lacost · Nov 25, 2012 at 11:30 PM
Uniti has Active function for this. SomeMonoBehaviour.Active(false) - will disable all scripts attached on game object (including render) and all of it's chields. I gues this is what you need.
PS. Active(bool val) - that's for unity 4. for 3. setActiveRecurseive(bool var) or smh like this, don't remember)
But that fully deactivates the object - it does not just make it invisible - the code in the Update function would not work using your suggestion.
as I understood from guastion, author do not need that's object att all until user click right arrow. But if some scripts on that gameobject sould work when it invisible GetComponentsInChildren - will be right design
Your answer
