Adding Exit to project

As part of a course that I am taking, I am working on my first project using Unity / C#. After following the step-by-step instructions, the project is complete and working as it is supposed to.

I would like to include a few additional things within the project, but since this is my first time working with Unity and C#, i am not sure as to the steps and scripting that is needed to complete adding an Exit function into the project.

The project has a Next page and Restart Text field (created using Game Object → UI → Text). I would like to add Exit to the project (created using the UI->Text), but am not sure as to the scripting aspect.

  • Do you add the Exit Script into the existing script (shown below) or is it better to create a new Script into the Assets and go from there?
  • If you add the Exit script into the existing script, where is it added?
  • And of course, what script write-up would you use?

I appreciate any help that anyone can provide. Here is the existing script of the project:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class GamePlay : MonoBehaviour, IDragHandler, IDropHandler {
public RectTransform background;
public RectTransform crntPainting;
public Button nextButton;
public Button restartButton;
public Text score;

int learnerResponse;
static int itemNumber;
static int numCorrect;
int[ ] answers = new int[ ] {-1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0};

// Use this for initialization
void Start () {
itemNumber = 1;
numCorrect = 0;
}

public void nextItem() {
itemNumber++;
print("itemNumber = " + itemNumber);
background.GetComponent().color = new Color(0.705882353f,0.705882353f,0.705882353f,1);

crntPainting.GetComponent().sprite = Resources.Load(“Images/image” + itemNumber, typeof(Sprite)) as Sprite;
crntPainting.position = new Vector2(512,384);

if (itemNumber == 11) {
nextButton.interactable = false;
}
}

public void restartGame() {
itemNumber = 1;
numCorrect = 0;
background.GetComponent().color = new Color(0.705882353f,0.705882353f,0.705882353f,1);
score.text = “”;

crntPainting.GetComponent().sprite = Resources.Load(“Images/image” + itemNumber, typeof(Sprite)) as Sprite;
crntPainting.position = new Vector2(512,384);
nextButton.interactable = true;
}

public void OnDrag(PointerEventData eventData) {
crntPainting.position += new Vector3(eventData.delta.x, eventData.delta.y);
}

public void OnDrop(PointerEventData eventData) {
print(crntPainting.position.x + " " + crntPainting.position.y);

learnerResponse = -1;
if (crntPainting.position.x > 542) {
learnerResponse = 1;
print (“Rembrandt Response”);
}
else if (crntPainting.position.x < 482) {
learnerResponse = 0;
print (“NonRembrandt Response”);
}

if (learnerResponse != -1) {
if (learnerResponse == answers[itemNumber]) {
print (“Correct”);
numCorrect++;
background.GetComponent().color = new Color(0,1,0,1);
}
else {
print (“Incorrect”);
background.GetComponent().color = new Color(1,0,0,1);
}
score.text = numCorrect + “/” + itemNumber;
}
}
}

I would do it via a UI button, not a UI text.
Game Object → UI → Button

then use the event system OnClick() that comes with the UI button to point to a function called ExitBtnPressed()

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class GamePlay : MonoBehaviour, IDragHandler, IDropHandler
{
    public RectTransform background;
    public RectTransform crntPainting;
    public Button nextButton;
    public Button restartButton;
    public Text score;


    int learnerResponse;
    static int itemNumber;
    static int numCorrect;
    int[] answers = new int[] {-1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0};

    // Use this for initialization
    void Start ()
    {
        itemNumber = 1;
        numCorrect = 0;
    }

    public void nextItem()
    {
        itemNumber++;
        print("itemNumber = " + itemNumber);
        background.GetComponent<Image>().color = new Color(0.705882353f,0.705882353f,0.705882353f,1);

        crntPainting.GetComponent<Image>().sprite = Resources.Load("Images/image" + itemNumber, typeof(Sprite)) as Sprite;
        crntPainting.position = new Vector2(512,384);

        if (itemNumber == 11)
        {
            nextButton.interactable = false;
        }
    }

    public void restartGame()
    {
        itemNumber = 1;
        numCorrect = 0;
        background.GetComponent<Image>().color = new Color(0.705882353f,0.705882353f,0.705882353f,1);
        score.text = "";

        crntPainting.GetComponent<Image>().sprite = Resources.Load("Images/image" + itemNumber, typeof(Sprite)) as Sprite;
        crntPainting.position = new Vector2(512,384);
        nextButton.interactable = true;
    }

    public void OnDrag(PointerEventData eventData)
    {
        crntPainting.position += new Vector3(eventData.delta.x, eventData.delta.y);
    }

    public void OnDrop(PointerEventData eventData)
    {
        print(crntPainting.position.x + " " + crntPainting.position.y);

        learnerResponse = -1;
        if (crntPainting.position.x > 542)
        {
            learnerResponse = 1;
            print ("Rembrandt Response");
        }
        else if (crntPainting.position.x < 482)
        {
            learnerResponse = 0;
            print ("NonRembrandt Response");
        }

        if (learnerResponse != -1)
        {
            if (learnerResponse == answers[itemNumber])
            {
                print ("Correct");
                numCorrect++;
                background.GetComponent<Image>().color = new Color(0,1,0,1);
            }
            else
            {
                print ("Incorrect");
                background.GetComponent<Image>().color = new Color(1,0,0,1);
            }
            score.text = numCorrect + "/" + itemNumber;
        }
    }
   
    public void ExitBtnPressed()
    {
        Application.Quit();
    }
   
}
1 Like

Thanks for the help… I will play with this tomorrow and see if I can get it to work.