Hide/Unhide Game Object

Hello there,

I have read numerous Unity posts and seem to have the issue still after trying all the suggested solutions.

Before I explain my issue, I will give a quick overview of what I am trying to accomplish, in case there is some other solution that makes more sense rather than what I am trying to do.

I want to have a Game Object appear at the beginning of the round in my game at one of seven or eight possible locations. The locations would be predefined Vector3’s (location in the game world) stored into an array and one would be randomly chosen at the beginning of each round. The object would then spawn at one of those locations, and the game object would then disappear until the next round. Essentially it would be a re-usable game object that could either be deleted, or hidden inbetween rounds.

What I have tried so far is the following…

  1. Instantiating the game object at the beginning of the game, and trying to hide it using object.active = false until the first round began, then at which I would set object.active = true. I then tried to delete the object using delete(object) and also deleteimmediate(object, true). I found that the deletion of the game object was not viable solution (as far as I am aware and understood) because it seemed that once it was deleted I could not “bring it back”

  2. Tried to have the game object in the scene at the beginning of the game, therefore no instantiating it and doing the same idea of object.active =true/false

  3. Tried to use object.GetComponent().enabled = false

  4. Tried to use object.GetComponent().enabled = false (or something similar)

  5. Tried to use object.setActive = false

As far as I am aware there is no parent setting that is causing an issue with this as it is not under anything in the hierarchy. The object that I was working with to make disappear was a primitive Sphere.

Here are code snippets of what I have been trying… These are not all going at once but rather just a list of different things I have tried which relate to items 1-5 above.

//DestroyImmediate(cat, true);

public void Despawn()
{
cat.GetComponent().enabled = false;
}

public void Spawn()
{
int i = 0;
i = Random.Range(0, spawnPoints.Length);

    cat.transform.position = spawnPoints*.position;*

cat.GetComponent().enabled = true;
//Instantiate(cat, catStartPos, Quaternion.identity);
//cat.active = false;
}

//Instantiate(cat, catStartPos, Quaternion.identity);
//cat.GetComponent().enabled = true;

So if anyone has any suggestions as to what to try or how to do this better/smarter I would be appreciative for any help. I know what I am trying to do is not impossible and I am likely just not seeing it right now. I am new to Unity but not new to game dev.
Thank you!

Hey!

If i understand correctly you want to hide/unhide a gameobject, right?

Then you just have to do this:

GameObject cat;
cat.SetActive(false); // false to hide, true to show

It does the same as clicking on the toggle next to the object’s name in the inspector.

This (below) would work too but only hide the object visually, it would still be there and every other script on it (like colliders, or your own scripts) would still be running.

cat.GetComponent<Renderer>().enabled = false;

If you want to reuse this object later, don’t destroy it. You will have to re instantiate it and it’s a waste of resource.

@nights007 is correct in that deactivating the gameObject is not the same as not rendering the object. This will turn off the MeshRenderer, the same as clicking the renderer off on the inspector. Try this:

MeshRenderer mr = go.GetComponent<MeshRenderer>();
mr.enabled = false;

NO, Not the same as not rendering. This completely temporarily removes the object from the scene, so it will be completely inactive. What he wants to do is temporarily not render it. There is no simple way to do this.

I was trying to do the same thing (which is why I’m here) and realized that it’s more effective to use:

bool onOff;
your logic here...
yourGameObject.SetActive(onOff);

and separate your logic (inventory data, for example) from your visuals (the thing you’re turning on and off, like an inventory screen). Then just update the visuals to reflect the logic once you’ve re-activated said visuals. So the logic itself is never de-activated. One way to do this is using Events. Code Monkey has a good video on events on YouTube.,I was trying to do the same thing (which is why I’m here) and realized that it’s more effective to use:

bool onOff;
your logic here...
yourGameObject.SetActive(onOff);

and separate your logic (inventory data, for example) from your visuals (the thing you’re turning on and off, like an inventory screen). Then just update the visuals to reflect the logic once you’ve re-activated said visuals. So the logic itself is never de-activated. One way to do this is using Events. Code Monkey has a good video on events on YouTube.