Separate scripts to destroy cube an display question, not working?

Hi,

I am trying to assign 1 question to 1 cube. In one script I have coded individually the trigger for the cube to disappear and in another script for the question to appear on screen upon collision.

At the moment the question appears on screen when the game starts and when I collide into the box the question doesn’t change?

Not sure what to do? :frowning:

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

public class Trigger : MonoBehaviour
{
    public GameObject guiObject;
    private Question1 guiScript;

    void Awake ()
    {
        guiScript = guiObject.GetComponent<Question1> ();
    }

    void OnTriggerEnter (Collider other)
    {
        guiScript.question1 = true;
        Destroy (this.gameObject);
    }
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Question1 : MonoBehaviour {
   
    private Rect windowRect = new Rect (500, 100, 400, 200); //Window size
    public bool question1;
   
    void OnGUI()
    {
        windowRect = GUI.Window (0, windowRect, WindowFunction, "Ebola Quiz Island"); //window on screen
    }
   
   
    void WindowFunction (int windowID)
    {
        // Draw any Controls inside the window here
        GUI.Label(new Rect (30, 25, 200, 50), " What year did Ebola begin?"); // Question
       
        if (GUI.Button (new Rect (20, 100, 100, 30), "1976")); // Answer buttons
        if (GUI.Button (new Rect (280, 100, 100, 30), "1986")); // 1st value right and left, 2nd value up and down,
        if (GUI.Button (new Rect (20, 150, 100, 30), "1996"));
        if (GUI.Button (new Rect (280, 150, 100, 30), "1966"));
    }
   
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Trigger2 : MonoBehaviour
{
    public GameObject guiObject;
    private Question2 guiScript;
   
    void Awake ()
    {
        guiScript = guiObject.GetComponent<Question2> ();
    }
   
    void OnTriggerEnter (Collider other)
    {
        guiScript.question2 = true;
        Destroy (this.gameObject);
    }
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Question2 : MonoBehaviour {
   
    private Rect windowRect = new Rect (500, 100, 400, 200); //Window size
    public bool question2;
   
    void OnGUI()
    {
        windowRect = GUI.Window (0, windowRect, WindowFunction, "Ebola Quiz Island"); //window on screen
    }
   
   
    void WindowFunction (int windowID)
    {
        // Draw any Controls inside the window here
        GUI.Label(new Rect (30, 25, 300, 50), " What is the Ebola virus named after?"); // Question
       
        if (GUI.Button (new Rect (20, 100, 100, 30), "A river")); // Answer buttons
        if (GUI.Button (new Rect (280, 100, 100, 30), "A farm")); // 1st value right and left, 2nd value up and down,
        if (GUI.Button (new Rect (20, 150, 100, 30), "A tree"));
        if (GUI.Button (new Rect (280, 150, 100, 30), "A city"));
    }
   
}

If I assume correctly what you’re trying to achieve, you should change your Trigger script in this way:

void Awake ()
    {
        guiScript = guiObject.GetComponent<Question1> ();
        guiScript.enabled = false; //Turn off the script, so the OnGUI function doesn't draw the question window
    }
    void OnTriggerEnter (Collider other)
    {
        guiScript.enabled = true; //Turn on the script (I think your line was just wrong)
        Destroy (this.gameObject);
    }

Same modification for Trigger2 and it should be okay!

1 Like

Thank you very much. :slight_smile: