If you want the cube to be destroyed when the UI with the question appears, then you have to create a new game object (it can be an empty object, so it is not drawn on the screen) and attach your GUIScreen script to that object. Make an âEbolaâ variable public in the GUIScreen script. Then create a reference to the GUIScreen in the âTriggerâ script and finally in the OnTriggerEnter function set the âEbolaâ variable of the GUIScreen class to true (before destroying the cube object).
Alternatively you could move the GUI code to the âTriggerâ script and when the trigger is activated (in the OnTriggerEnter function) you would disable the renderer and collider components of the cube and then when the user will click on one of the answer buttons, you could destroy the cube object.
And finally if you are planning to do a UI heavy game, I would strongly recommend to use our new UI instead of the legacy GUI. You can learn about it here: Learn game development w/ Unity | Courses & tutorials in game design, VR, AR, & Real-time 3D | Unity Learn
So your scripts will look something like this.
using UnityEngine;
using System.Collections;
public class Trigger : MonoBehaviour
{
public GameObject guiObject;
private GUIScreen guiScript;
void Awake ()
{
guiScript = guiObject.GetComponent<GUIScreen> ();
}
void OnTriggerEnter (Collider other)
{
guiScript.Ebola = true;
Destroy (this.gameObject);
}
}
and
using System;
using System.Collections.Generic;
using UnityEngine;
using System.Collections;
public class GUIScreen : MonoBehaviour
{
public bool Ebola;
// your other code to show questions
}
Then at the end of your âHandleAnswerâ function set the Ebola variable to false.
Select your cube in Unity editor and in the âInspectorâ panel you will see a âguiObjectâ value. From the âHierarchyâ panel drag the ui object (which has âGUIScreenâ script attached to it) to that âguiObjectâ value.
This should work.
Also you could tag your ui object (e.g. âQuestionsâ) and then instead of
guiScript = guiObject.GetComponent<GUIScreen> ();
you could use
guiScript = GameObject.FindWithTag("Questions").GetComponent<GUIScreen> ();
This way you would not need a âpublic GameObject guiObject;â variable and would not need to drag the ui object in the editor. You can read about tags here Unity - Manual: Tags