Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
5
Question by greens356 · Apr 15, 2016 at 06:56 AM · gameobjectprefabspawninghidingon and off

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[i].position;
     cat.GetComponent<Renderer>().enabled = true;

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

... //Instantiate(cat, catStartPos, Quaternion.identity);

     //cat.GetComponent<Renderer>().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!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fafase · Apr 15, 2016 at 07:48 AM 0
Share

gameObject.active is deprecated so except if you are using an old version of Unity, you need to use gameObject.SetActive(value);

4 Replies

· Add your reply
  • Sort: 
avatar image
19
Best Answer

Answer by MingJ · Apr 15, 2016 at 08:02 AM

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image greens356 · Apr 15, 2016 at 11:26 PM 0
Share

Thank you for the help and response. I used the setActive() method and it worked, however, part of the issue was that I was referencing the prefab and not the actual game object that I had in the scene. So ins$$anonymous$$d of using the cookie, I was using the cookie cutter.

Thank you much.

avatar image MingJ greens356 · Apr 18, 2016 at 07:26 AM 0
Share

Ok, good. Never forget to instantiate your object in a variable if you're gonna use it later ^^

avatar image andreythegiant · Jul 18, 2020 at 03:47 AM 0
Share

thanks much love brotha

avatar image
2

Answer by jkraptor · Apr 17, 2018 at 10:40 PM

@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;


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by nights007 · Nov 07, 2017 at 10:33 AM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by danielbradley418 · May 14, 2021 at 08:58 AM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Destroying Game object (Not destroying) C# 1 Answer

What does "Couldn't find matching instance in prefab" mean? 2 Answers

How to get a referance to an instantiated prefab clone ? 1 Answer

Positions of Objects in Unity - World Position, Transform Position, Inspector Position, Local Position, Prefab Positions, Center of Objects for Parents & Childs 1 Answer

Only half of a prefab is spawning? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges