Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by barney_unity869 · Nov 06, 2019 at 02:39 PM · setactiveinstantiate prefab

SetActive(false) is preventing GameObjects from Instantiating

Hi,

I'm facing an issue where trying to hide a GameObject with SetActive(false) is preventing other GameObjects from instantiating. I've added some pseudocode below to illustrate my problem, basically I have a Cursor that can either select the player or open a menu, a Tilemap that can create new tiles (using a prefab) to show where the player can move, and a Menu that when clicked should select the player and then hide itself by deactivating.

As I'd expect, when I select the player the tiles all display correctly in the view as well as the hierarchy and the Log message also shows.

However, when I open the menu and click on it, the Log messages from the Tilemap will appear, showing that the tiles are being added, but no tiles appear either in the view or the hierarchy. If I remove the line gameobject.SetActive(false) then the tiles render as expected but the UIMenu doesn't disappear.

The Tilemap and UIMenu objects are completely separate within the hierarchy so I find it very odd that one is affecting the other.

 class Cursor {
 
     public Tilemap tilemap;
     public UIMenu uiMenu;
     
     public void OnSelect(string selection) {
         if (selection == "player") {
              SelectPlayer();
         } else if (selection == "menu") {
             SelectMenu();
         }
 
    }
 
    public void SelectPlayer() {
        tilemap.ShowMovableTiles();
    }
 
    public void SelectMenu() {
        uiMenu.Display(() => SelectPlayer())
    }
 }
 
 class Tilemap {
 
     public GameObject tile;
 
     public void ShowMovableTiles() {
           foreach(Transform position in SomeHowGetMovablePositions) {
                 Debug.Log("Adding new tile");
                 GameObject newTile = Instantiate(tile, position, Quaternion.identity);
                 newTile.gameObject.transform.SetParent(this.transform);
           }
     }
 }
 
 class UIMenu {
 
       public Button button;
       public System.Action action;
 
       public void Awake() {
             button = GetComponent<Button>();
             button.AddListener(OnClick);
      }
 
       public void Display(System.Action onClickHandler) {
              action = onClickHandler;
       }
 
      public void OnClick() {
             action();
             gameObject.SetActive(false); //hide menu
      }
 
 }
 








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 barney_unity869 · Nov 07, 2019 at 06:57 PM 0
Share

I've also tried logging appending newTile to a public list and logging the length of the list as tiles are added. What's really weird is that the length of the list increases as expected but in the inspector the list remains unchanged. I'm really stumped, does anyone have an idea what's going on?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by barney_unity869 · Nov 11, 2019 at 09:35 AM

I'm still not quite sure why this is happening, but I don't think it's related to the SetActive(false) call. I updated the way I hide UI elements by adding a CanvasGroup element and simply setting the alpha to 0 and turning off interactibility to make the UI gameobject disappear. Turns out that even doing this was preventing my gameobjects from instantiating.

I eventually solved the issue by hiding the UI on a time delay, Invoke("Hide", 0.01), feels a little hacky, but it works well!

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

187 People are following this question.

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

Related Questions

GameObject SetActive not reactivating 5 Answers

Activate/Deactivate image with GetKeyDown? 0 Answers

OnTriggerExit not reactivating a GameObject 1 Answer

how to do a if statement to activate and deactivate a button 1 Answer

Getting component from derived classes 0 Answers


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