Show/hide button

So now I’m thinking that I shouldn’t use z to have the text be behind the buttons, but instead have it be the mesh renderer be enabled or disabled depending on need, by tag, like I used to do in the other Show/Hide button.

https://docs.unity3d.com/ScriptReference/Renderer-enabled.html

I made the show/hide button work to render and not render the answers over the answer flashcards!

It requires that the answers be tagged as “Answer”, so the prefab will need to be updated for that. #todo

#todo – I would also like the text to look more child-friendly.

A cool thing is that in googling this rendering thing, I also found the function at the bottom, that toggles the rendering every second. I’d like to use that to remind users to hit buttons – or at least at the start, to hit the first answer button. #todo

There’s a chance I might be better off with a brief tutorial, but the flashing buttons could be a segue (or could be good in addition to it).

The next thing I want to do for the show/hide button is :

  • only have it appear when all of the box colliders for the answer slots are filled in (what if there has been a collision but it hasn’t *stayed* there)
  • and then have the name of the show button change to “hide” in context – and change the name to “check” instead of show
  • make the show/hide button look more attractive
  • make it flash when it shows up, and say “check your answers!”
  • after that, it’d be nice if there were a tally of how many correct slots you did – __/15.
  • but it’s unclear how much some of those goals matter in comparison to getting it massively expanded in terms of content

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Show : MonoBehaviour
{
    private Renderer rend;
    Button myButton;
    private GameObject[] AnswerTexts;

    void Start()
    {

        myButton = GetComponent<Button>(); // access button component 
        myButton.onClick.AddListener(() => ShowHide());
        AnswerTexts = GameObject.FindGameObjectsWithTag(Answer);
        Debug.Log (Answer);   

        foreach (GameObject answer in AnswerTexts) 
        {
            rend = answer.GetComponent<Renderer>();
            rend.enabled = true;

//                    r.enabled = false;
        }

    }
        

        void ShowHide()
        {
            
    //        GameObject[] consClusts = GameObject.FindGameObjectsWithTag(ConsCluster);
          
            foreach (GameObject answer in AnswerTexts)
            {
                
                if (answer.GetComponent<MeshRenderer>().enabled == true)
                {
                    answer.GetComponent<MeshRenderer>().enabled = false;
                    foreach (Renderer r in answer.GetComponentsInChildren<Renderer>())
                    {
                        r.enabled = false;
                    }
                }
    
                else if (answer.GetComponent<MeshRenderer>().enabled == false)
                    {
                    answer.GetComponent<MeshRenderer>().enabled = true;
                    foreach (Renderer r in answer.GetComponentsInChildren<Renderer>())
                        {
                            r.enabled = true;
                        }
    
                    }
            }
    
    
        }
    

//    // Toggle the Objects visibility each second.
//    void Update()
//    {
//        // Find out whether current second is odd or even
//        bool oddeven = Mathf.FloorToInt(Time.time) % 2 == 0;
//
//        // Enable renderer accordingly
//        rend.enabled = oddeven;
//    }
//}

}

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *