Show and Hide object ?

Updated on June 27, 2016 in Answers
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
12 on June 26, 2016

I have object I want to control it by show and hide it.


using UnityEngine;

using System.Collections;
public class DeadLife : MonoBehaviour {
public float O_Life = 100f;
// Use this for initialization

void Start () {

}
// Update is called once per frame

void Update (){
if(O_Life <= 90f)

{

gameObject.SetActive(false);

}
if(O_Life >= 90f)

{

gameObject.SetActive(true);

}
}

}

If O_Life <90 it hide. Good
But if i want to get it back “show or appear it again” it not work !?
So, How to fix this ?

  • Liked by
Reply
8 on June 26, 2016

Where is the gameobject you are trying to turn off? is you are just using gameObject.SetActive you are probably trying to turn off the game object holding the script. In this case you will not be able to turn it back on.

Helpful
on June 26, 2016

put your script on a different gameobject and add a public gameobject to the script and drag in the GO you want to turn off. like so:

 public GameObject controller;
 void Update ()
{
if(O_Life <= 90f)
{
 controller.SetActive(false);
 }
if(O_Life >= 90f)
 {
 controller.SetActive(true);
 }
}
 

on June 26, 2016

put your script on a different gameobject and add a public gameobject to the script and drag in the GO you want to turn off. like so:

 public GameObject controller;
 void Update ()
{
if(O_Life <= 90f)
{
 controller.SetActive(false);
 }
if(O_Life >= 90f)
 {
 controller.SetActive(true);
 }
}
 

From comphonia

It doesn’t work !! Does it work with you ?
I cant get the object back again ! its a bug !

Guru
on June 26, 2016

Did you put the script on the object that you are disabling? Then no you can’t enable it again. And it’s not a bug make sure to put it of a different gameobject

Helpful
on June 26, 2016

it works for me , try using buttons to test it first I think your problem is with the O_Life logic 

on June 26, 2016

OK. I’m not using buttons. Yes, with buttons it work as this image you post it (Thank you for that). What i do is change the number from the O_Life bar. If i reduce it under 90 it hide but if increase it above 90 it not get back !
I change the script and add this line 
gameObject.GetComponent<MeshRenderer>().enabled = false;

using UnityEngine;
using System.Collections;
public class DeadLife : MonoBehaviour {
public GameObject controller;
public float O_Life = 100f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update (){
if(O_Life <= 90f)
{
gameObject.GetComponent<MeshRenderer>().enabled = false; 
}
if(O_Life >= 90f)
{
gameObject.GetComponent<MeshRenderer>().enabled = true; 
}
}
}

it run without any problem ! even with or without write “public GameObject controller;”

Thank you so much for all this information.

on June 27, 2016
I see my way worked? 😛From InvisionCreations

 its work yes. but hide the MeshRenderer only but the object is there. it give me like invisble wall. yes you can not see it but its there and i don’t want this ^-^. Its work fine if i changed the numbers instead “setactive” !
Thank you guys for you help. I will keep coding until find way to solve this.

Show more replies
  • Liked by
Reply
Cancel
0 on June 26, 2016

try to use 

else
{
controller.SetActive(true);
}

  • Liked by
Reply
Cancel
0 on June 26, 2016

anyway, I think its bug. I will find other way to know whether the object is dead or not.
Thank you so much guys.

  • Liked by
Reply
Cancel