- Home /
The question is answered, right answer was accepted
hide object start script
what i want to do is hide the child at the start, then when "B" is pressed the object appears. however i don't know how to make it hide at the start. my awful attempt is below:
function Start (child.renderer.enabled = false;)
function OnTriggerStay(trigger : Collider) {
if((trigger.gameObject.tag == "Player") && Input.GetKeyDown(KeyCode.B)) {
var child = transform.GetChild(0);
child.renderer.enabled = true;
}
}
i know this doesnt work, can someone show me how to write this properly
thanks
Have you multiple objects to disable at the beginning? or juste a specific one?
So you want it to reappear when the user presses the B key? If so, why are you using an OnTriggerStay Function? That kind of thing would be better served in an Update function.
yes,the second part works fine i have tested it, i just need to know how to make the child hide at the start
You'll either have to declare and initialize the child variable earlier in your script (I.$$anonymous$$ before the Start function) OR set the child's renderer to disabled in the scene view (Look for it in the inspector, should be a little checkbox).
Answer by EvilTak · Oct 21, 2014 at 04:53 PM
Best approach would be this:
void Start(){
child.renderer.enabled = false;
}
void Update(){
if(Input.GetKeyDown("b"))
child.renderer.enabled = true;
}
thanks for the help. i did not use your answer. i used
function Start(){
var child = transform.GetChild(0);
child.renderer.enabled = false;
}
cheers
Answer by g8minhquan · Oct 21, 2014 at 04:54 PM
function Start ( )
{
var child = transform.GetChild(0);
child.renderer.enabled = false;
}
Answer by Diablo404 · Oct 21, 2014 at 06:49 PM
Well, you didn't really answered my question. But, let's assume you have just one. You could use: GameObject.Find("NameOfYourObject").renderer.enabled = false; It's dirty but should work.
Regarding your code you could also do:
GameObject.FindWithTag("Player").transform.GetChild(0).renderer.enabled = false. Once again, this one is dirty and would not work if you have multiple gameobjects with the "Player" tag.
Finally, the best way is to declare a variable as GameObject. Fill it in the inspector and then call it in start like so: var myChild : GameObject; function Start { myChild.renderer.enabled = false; }
Answer by unitygamer · Oct 21, 2014 at 06:59 PM
do you have scripts attached to the 'child' that you would use ? or else you could just assign the child to a variable via inspector and use
var myChild:GameObject; function Start () { myChild.gameObject.SetActive (false); }
note:this disables the entire child including the scripts on it.
or you could try carivorousHam's answer by setting the renderer checkbox to false(untick) in the inspector when outside playmode and enable the renderer as in your above script
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
hide child object script - help 1 Answer
button question 1 Answer
Need Help with rebuildable barricade system like in Call Of Duty Zombies 1 Answer
Help Solve This 2 Answers