First time doing Popups


The Goal

I wanted to use pop-ups for the storing of the creator credits for the game, and for a caution notification when someone tries to reset the highscore of BICS.

The Code

This one turns out to be pretty simple: you need 2 code, one on the button bringing the pop-up out, and one on the button closing the pop-up:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PopupScript : MonoBehaviour
{
    public GameObject thePopup;//just drag the correct gameObject/canvas to the slot in the Inspector
    public void Popup()//place this function in the OnClick() for the button the script is attached to
    {
        thePopup.SetActive(true);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PopupExitScript : MonoBehaviour
{
    public GameObject thePopup;//just drag the correct gameObject/canvas to the slot in the Inspector
    public void ClosePopup()//place this function in the OnClick() for the button the script is attached to
    {
        thePopup.SetActive(false);
    }
}


Example showing how the On Clcik() in the inspector has to be altered for the scripts to work (only including the Opening of popup. The other part has to be added to an exit button in the pop-up itself (called "GameObject" in my case))

That's all! It's that simple. Do remember to set the gameObject that is the pop-up to Inactive, so it would not appear until the Popup() is called. (Uncheck the box next to the name "GameObject" at the very top of the inspector.

  

Get Bull in a china shop

Leave a comment

Log in with itch.io to leave a comment.