Search Unity

How to Hide or Unhide GameObject with button

Discussion in 'Scripting' started by Goldensnitch, Dec 2, 2016.

  1. Goldensnitch

    Goldensnitch

    Joined:
    Nov 4, 2016
    Posts:
    60
    Hello guys i am making 2 simple buttons that will appear when i touch the object. When i touch that button it will able to hide an object and unhide the object that i selected. How do i do this please help me. Problem is i dont know how to script this. Also i got 320 objects in my game screen. Please help me to solve this problem Thank you. Its much similer to picture below
     

    Attached Files:

    Last edited: Dec 2, 2016
  2. romatallinn

    romatallinn

    Joined:
    Dec 26, 2015
    Posts:
    161
    1. Create a single script for all objects
    2. there you can make the header for the window, or description of the object. Just some info about it...
    3. Also, create function OnMouseDown. Inside it, include code that will show the window. Give all needed information into this window. One of it will be GameObject itself. So, window has an link to the object.
    4. So, now when you click hide or unhide, the window set active the object accordingly.

    NB! Dont forget about colliders on the objects.
     
    Last edited: Dec 2, 2016
    khushbu2410 likes this.
  3. Goldensnitch

    Goldensnitch

    Joined:
    Nov 4, 2016
    Posts:
    60
    Thank you for your concern. I am stuck with scripts :p
     
  4. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    You don't need any scripting for this.
    1: Select UI/Button from the Create Menu
    2: Select the created Button. On the Button component in the Inspector, there is an area that is labeled "On Click()", click the + button here to add an action to this event.
    3: Drag the object you want clicking this button to disable into the empty object field in the new action you created.
    4: On the Dropdown that currently has "No Function" selected, choose GameObject/SetActive(bool)
     
    Zandarn, Mythrintius, pren1 and 5 others like this.
  5. Goldensnitch

    Goldensnitch

    Joined:
    Nov 4, 2016
    Posts:
    60
    But i got 335 objects
     
  6. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    Parent them to one object, and turn it off.
     
  7. Goldensnitch

    Goldensnitch

    Joined:
    Nov 4, 2016
    Posts:
    60
    Thanks Damoen what how do i turn it off
     
  8. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    Create an empty GameObject, parent them all to that. You disable it with a button as detailed above.
     
  9. Goldensnitch

    Goldensnitch

    Joined:
    Nov 4, 2016
    Posts:
    60
    What action will hide the object that i select in On Click?
     
  10. romatallinn

    romatallinn

    Joined:
    Dec 26, 2015
    Posts:
    161
    GameObject -> SetActive -> false (unchecked box)

    NB! If you disable the object, then all scripts that are hanging on it or on the children will not work. So, you have to ensure that important scripts will not be disabled.
     
    muyak likes this.
  11. Goldensnitch

    Goldensnitch

    Joined:
    Nov 4, 2016
    Posts:
    60
    Thank you Romatllinn Can you help me find these scripts?
     
  12. romatallinn

    romatallinn

    Joined:
    Dec 26, 2015
    Posts:
    161
    I meant, your custom important scripts. Don't worry about it, if the objects don't use any of custom scripts. If it does use some, then it is still not a huge issue. Analyse the code and think "if I disable this script, will it broke my app?". I guess, since they are just small objects that are needed to render meshes, nothing important there should be (like game logic, some static fields that must be accessed while app is running, etc).
     
  13. Goldensnitch

    Goldensnitch

    Joined:
    Nov 4, 2016
    Posts:
    60
    sorry mate my brain doesnt function right cant understand what you say
     
  14. romatallinn

    romatallinn

    Joined:
    Dec 26, 2015
    Posts:
    161
    Did you make the app work? Now you are able to disable and enable the objects?

    About the important scripts. Lets imagine a bone that you want to disable on click. Do you have on this bone any custom/made by yourself scripts? If yes, then does it contain important function or game logic? For example, if you proceed, I do not know, for example, score count. If you disable this bone with this script, the scores will not be counted until you enable the object. So, I just meant, that you have to consider that the scripts on the disabled objects do not work.
     
  15. Goldensnitch

    Goldensnitch

    Joined:
    Nov 4, 2016
    Posts:
    60
    Romatallinn thank you for your time please check this link https://forum.unity3d.com/threads/please-help-me-to-build-mobile-application.444451/#post-2874801
     
  16. Goldensnitch

    Goldensnitch

    Joined:
    Nov 4, 2016
    Posts:
    60
    I found this script but unity 5.5.0 cant recognize this function thing. I am having error like this "Assets/3dmodel/newscripts/hide.cs(6,2): error CS0246: The type or namespace name `function' could not be found. Are you missing an assembly reference?"
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class hide : MonoBehaviour {
    6.     function Update() {
    7.  
    8.         if (Input.GetKeyDown(KeyCode.Z)) {
    9.             // show
    10.             // renderer.enabled = true;
    11.             gameObject.GetComponent<Renderer>().enabled = true;
    12.         }
    13.  
    14.         if (Input.GetKeyDown(KeyCode.X)) {
    15.             // hide
    16.             // renderer.enabled = false;
    17.             gameObject.GetComponent<Renderer>().enabled = false;
    18.         }
    19.     }
    20. }
    21.  
     
  17. gcoope

    gcoope

    Joined:
    Dec 20, 2012
    Posts:
    11
    Replace function with Void, function is from JavaScript and you're using C#.
     
  18. tmt102

    tmt102

    Joined:
    Feb 17, 2017
    Posts:
    1
    hope it's useful
    public GameObject adddomi;
    bool m_Activate
    void Start () {
    m_Activate = false;
    adddomi.gameObject.SetActive(m_Activate);
    }
    void Update () {
    }

    public void showdomino()
    {

    if (m_Activate == false) {
    adddomi.gameObject.SetActive (true);
    m_Activate = true;
    }
    else
    {
    adddomi.gameObject.SetActive (false);
    m_Activate = false;
    }
    }
    }
     
  19. madeye92

    madeye92

    Joined:
    Jun 17, 2018
    Posts:
    1
    I am Trying to do something similar to the original post here, I have many photo albums that I am placing onto a Panel/UI and each photo album is a child of the same panel rather than have a different panel for each one. so when I use menu buttons to choose which album to set as active I have to set every other album to false on every button. I am looking to expand this to many albums and I can see that each button for each album is going to have a onClick setActive false for every other album. My question is, is there a way to set all the other albums to false in one line of code and then just have the one true statement as well. so that each button only needs two onClick functions.
     
  20. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Just keep track of the currently active GameObject in script. Deactivate that one, and then activate the new one.

    You could also use a script to loop through all of the GameObjects and disable them all.
     
  21. arvind1359

    arvind1359

    Joined:
    Sep 11, 2019
    Posts:
    4
    Hi everyone,
    Can anyone help me out in creating a c# code which contains "Adding & deleting an object on tapping in the presence of another object.
     
  22. admin_unity486

    admin_unity486

    Joined:
    Oct 16, 2019
    Posts:
    5
    hey mate is their anyway of clicking the button again to make the object appear again ?
     
  23. JohnSu6616

    JohnSu6616

    Joined:
    Apr 9, 2013
    Posts:
    3
    nice tutorial to show and hide gameobject.
     
  24. YerloOrley

    YerloOrley

    Joined:
    Jun 21, 2020
    Posts:
    3
    //habilitar y deshabilitar Render y todos los Colliders

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class VisibleObject : MonoBehaviour
    {

    public void ToggleVisibility()
    {
    Renderer rend = gameObject.GetComponent<Renderer>();

    if(rend.enabled)
    {
    rend.enabled = false;
    //deshabilita todos los colliders
    foreach(Collider m_Collider in gameObject.GetComponentsInParent<Collider>()){
    m_Collider.enabled = false;
    }
    }
    else
    {
    rend.enabled = true;

    foreach(Collider m_Collider in gameObject.GetComponentsInParent<Collider>()){
    m_Collider.enabled = true;
    }
    }
    }
    }
     
    jingk03 likes this.