Search Unity

Show an inactive Gamobject by button

Discussion in 'Scripting' started by StephanUMC, Oct 20, 2017.

  1. StephanUMC

    StephanUMC

    Joined:
    Sep 11, 2017
    Posts:
    12
    Hi,

    I want to toggle the visibility of a gameobject by pressing a button. My script is able to hide my gameobject (by pressing Z), but I'm not able to show the gameobject again. Can anyone help me?

    void Update()
    {
    if (Input.GetKeyDown(KeyCode.Z))
    {
    // show
    // renderer.enabled = true;
    gameObject.SetActive(false);
    GameObject.Find("HoloLensCamera").GetComponent<GraphManager>().enabled = false;
    }

    if (Input.GetKeyDown(KeyCode.V))
    {
    // hide
    // renderer.enabled = false;
    gameObject.SetActive(true);
    GameObject.Find("HoloLensCamera").GetComponent<GraphManager>().enabled = true;
    }
    }
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Please look at this page for how to insert code nicely into the forums: https://forum.unity.com/threads/using-code-tags-properly.143875/

    Once your game object is active, you cannot use the 'v' key to activate it again because the update function won't be running on that game object.
    With a button, you can target a method in the OnClick unity event that would do the equivilent of your GetKeyDown for KeyCode.V :) In fact, if you made the method inside that script, you could then call it for the keydown (replace), as well as the OnClick of the button ;)
    Unless you didn't want to do the other lines of code at the same time, then you could just assign the game object to the OnClick UnityEvent and without any code, just choose 'game object' in the drop down menu and the set active bool that's in the list :)
     
  3. StephanUMC

    StephanUMC

    Joined:
    Sep 11, 2017
    Posts:
    12
    Thanks for the fast reply! Change of plans, instead of changing the visibility by pressing a button I want to change an invisible gameobject to a visible one with an airtap in my hololens. What am I doing wrong?

    Code (CSharp):
    1. using HoloToolkit.Unity.InputModule;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class NewBehaviourScript : MonoBehaviour,IInputClickHandler {
    7.  
    8.     public void OnInputClicked(InputClickedEventData eventData)
    9.     {
    10.         gameObject.SetActive(true);
    11.         GameObject.Find("HoloLensCamera").GetComponent<GraphManager>().enabled = true;
    12.     }
    13.  
    14.    
    15. }
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, is the game object that has that script different from the one you want to make active?
    My first guess would be that it is.. :)

    If it's not, you're probably not getting that InputClicked method called.

    If it's a different game object you want to set active, what you should do is make a variable for it, and connect it in the inspector.
    Code (csharp):
    1.  // inside the class, but not the method..
    2. [SerializeField]
    3. GameObject objectToShow;  // in the editor, look at the script in the inspector and drag the object here :)
    4. // inside the method
    5. objectToShow.SetActive(true);
    6.  
    Note about your GameObject.Find -- better to link that in the inspector also.
    If for any reason that's not easily possible, I'd suggest looking for it in Start and storing it in a variable for use later on. That could be research for another time on how to get its reference in better ways :)
     
  5. StephanUMC

    StephanUMC

    Joined:
    Sep 11, 2017
    Posts:
    12
    I have got an empty GameObject with the script. I have changed my code and dragged my cube which I want to show in the inspector, but it is still not working. Hope you can help me.

    Stephan
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I've never developed for hololens stuff.. but does this script work if you put it on , say a cube?

    And just to double check your code, could you post the updated code?
     
  7. StephanUMC

    StephanUMC

    Joined:
    Sep 11, 2017
    Posts:
    12
    I've added the script to a cube but it's doesn't work to make it invisible with an airtap. Hope you can help me.

    Code (CSharp):
    1. using HoloToolkit.Unity.InputModule;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class NewBehaviourScript : MonoBehaviour,IInputClickHandler {
    7.  
    8.     // inside the class, but not the method..
    9.     [SerializeField]
    10.     GameObject objectToShow;  // in the editor, look at the script in the inspector and drag the object here :)
    11.                            
    12.    
    13.  
    14.     public void OnInputClicked(InputClickedEventData eventData)
    15.     {
    16.         objectToShow.SetActive(false);
    17.         //GameObject.Find("HoloLensCamera").GetComponent<GraphManager>().enabled = true;
    18.     }
    19.  
    20.    
    21. }
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I'm at a loss, sorry.. I did quickly google what it even means to air tap or that handler, but I got the impression that if you're gazing at the object and airtap (whatever that is?!lol) then you get that method from the interface called. Since I've never used it, I can't say if you're missing something.

    Okay, 1 more quick-ish google and got this: http://chadcarter.net/activating-and-deactivating-objects-in-unity-using-holotoolkit-inputmanager/

    Looks very similar to what you're trying. I guess make sure you did the steps that he does in the video and see if it works for you. Seems simple, from the parts I watched. He uses some prefab cameras and cursors from the holo toolkit and 2 small pieces of code (and 2 game objects) :)
     
  9. StephanUMC

    StephanUMC

    Joined:
    Sep 11, 2017
    Posts:
    12
    Thanks! The script is now working when I add it to a cube for example. In my scene I have added a cube to the main camera so it is always in my field of view. My problem now is that you can't use this method because the raycast never hits the object in this way. Is there a way to use this method with every airtap (so not only on the specific gameobject)?
     
  10. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    In which way - to hide it or make it visible? If it's to hide it, I imagine you could put that script on any object and it would work. If it's to show, that's different (at least from the example), because it shows looking or clicking on a certain object to make a list of game objects visible.
     
  11. StephanUMC

    StephanUMC

    Joined:
    Sep 11, 2017
    Posts:
    12
    You're right, I needed an extra gameobject to show my cube again, it is working right now. Thanks for the help!
     
  12. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    No problem :) Glad you got it working.