- Home /
Disable the part of the gameobject
I have a gameobject (Imported FBX file) with Groups. I want to set visibility off of one of the group. I tried following code but not working.
private GameObject headBoard;
void Start()
{
headBoard=GameObject.Find("Group1");
}
void Update()
{
headBoard.renderer.enabled=false;
}
I am not able to Hide that specific group. Can anyone please help me..???
Answer by SilverFoxMedia · Jul 04, 2012 at 07:28 AM
If you're trying to disable the group you could just make it inactive using headBoard.SetActiveRecursively(false); unless of course you have a specific reason for just disabling the renderer.
using UnityEngine; using System.Collections;
public class disableGroup : MonoBehaviour { private GameObject headBoard;
void Start () {
headBoard = GameObject.Find("Group1");
}
void Update () {
if (headBoard.active) {
headBoard.SetActiveRecursively(false);
}
}
}
It's better to use relative paths and work with references. GameObject.Find will search the whole scene. You should hold a reference to the child you want to deactivate, or at least a reference to the root object.
Use Transform.Find to get a child object from another object.
For example if your script is attached to the gameobject in question, just use:
transform.Find("Group1");
$$anonymous$$eep in $$anonymous$$d that you might have a deeper hierarchy, so check where your desired child is and whether the name is correct.
So performance wise I should use Transform.Find...????
Well, it's not that performance relevant. It's more about unambiguity of the object you want to access. In the real world this
GameObject.Find("Left Foot")
would say "search the whole earth and give my any "Left Foot" you can find".
transform.Find("Left Foot")
would say: give me the child of this object that is named "Left Foot".
Answer by fafase · Jul 04, 2012 at 07:25 AM
If what you want is activate and deactivate object and its children, you can use
gameObject.SetActiveRecursively(true or false);
Set false for having it all off.
Your answer
Follow this Question
Related Questions
Toggling a script on/off using the keyboard? 2 Answers
Disabling a script . 1 Answer
Enabling/Disabling Multiple Tagged objects 1 Answer
enable/disable specific components 3 Answers
Disable a script on another game object 2 Answers