Hide or Show Canvas elements in Unity C#
Hello coders, in this tutorial, you will learn how to hide or Show Canvas elements in Unity using C# programming.
Before we start with the building process, we need to know a few things. Let’s first discuss them.
GameObject.SetActive
It Activates or Deactivates the gameObject depending on the given value.
- If we give True as a value it Activates.
- If we give False as a value it Deactivates.
After a GameObject gets Deactivated all the components are disabled, including attached scripts, colliders, renderers, and rigidbodies.
Syntax:
public void SetActive(bool value);
C# Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CanvasScript : MonoBehaviour
{
public GameObject canvas;
// Start is called before the first frame update
public void ShowCanvas()
{
// Activates the GameObject
canvas.SetActive(true);
}
// Update is called once per frame
public void HideCanvas()
{
// Deativates the GameObject
canvas.SetActive(false);
}
}
Add this Script to the Canvas UI
Output:
If we run this Unity scene, you will be able to see what is given in the video screenshot below:
Leave a Reply