Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

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

Discussion in 'Editor & General Support' started by RKM_91, Apr 2, 2015.

  1. RKM_91

    RKM_91

    Joined:
    Oct 27, 2013
    Posts:
    28
    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? :(

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class Trigger : MonoBehaviour
    6. {
    7.     public GameObject guiObject;
    8.     private Question1 guiScript;
    9.  
    10.     void Awake ()
    11.     {
    12.         guiScript = guiObject.GetComponent<Question1> ();
    13.     }
    14.  
    15.     void OnTriggerEnter (Collider other)
    16.     {
    17.         guiScript.question1 = true;
    18.         Destroy (this.gameObject);
    19.     }
    20. }
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class Question1 : MonoBehaviour {
    6.    
    7.     private Rect windowRect = new Rect (500, 100, 400, 200); //Window size
    8.     public bool question1;
    9.    
    10.     void OnGUI()
    11.     {
    12.         windowRect = GUI.Window (0, windowRect, WindowFunction, "Ebola Quiz Island"); //window on screen
    13.     }
    14.    
    15.    
    16.     void WindowFunction (int windowID)
    17.     {
    18.         // Draw any Controls inside the window here
    19.         GUI.Label(new Rect (30, 25, 200, 50), " What year did Ebola begin?"); // Question
    20.        
    21.         if (GUI.Button (new Rect (20, 100, 100, 30), "1976")); // Answer buttons
    22.         if (GUI.Button (new Rect (280, 100, 100, 30), "1986")); // 1st value right and left, 2nd value up and down,
    23.         if (GUI.Button (new Rect (20, 150, 100, 30), "1996"));
    24.         if (GUI.Button (new Rect (280, 150, 100, 30), "1966"));
    25.     }
    26.    
    27. }
    28.  
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class Trigger2 : MonoBehaviour
    6. {
    7.     public GameObject guiObject;
    8.     private Question2 guiScript;
    9.    
    10.     void Awake ()
    11.     {
    12.         guiScript = guiObject.GetComponent<Question2> ();
    13.     }
    14.    
    15.     void OnTriggerEnter (Collider other)
    16.     {
    17.         guiScript.question2 = true;
    18.         Destroy (this.gameObject);
    19.     }
    20. }
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class Question2 : MonoBehaviour {
    6.    
    7.     private Rect windowRect = new Rect (500, 100, 400, 200); //Window size
    8.     public bool question2;
    9.    
    10.     void OnGUI()
    11.     {
    12.         windowRect = GUI.Window (0, windowRect, WindowFunction, "Ebola Quiz Island"); //window on screen
    13.     }
    14.    
    15.    
    16.     void WindowFunction (int windowID)
    17.     {
    18.         // Draw any Controls inside the window here
    19.         GUI.Label(new Rect (30, 25, 300, 50), " What is the Ebola virus named after?"); // Question
    20.        
    21.         if (GUI.Button (new Rect (20, 100, 100, 30), "A river")); // Answer buttons
    22.         if (GUI.Button (new Rect (280, 100, 100, 30), "A farm")); // 1st value right and left, 2nd value up and down,
    23.         if (GUI.Button (new Rect (20, 150, 100, 30), "A tree"));
    24.         if (GUI.Button (new Rect (280, 150, 100, 30), "A city"));
    25.     }
    26.    
    27. }
    28.  
     
  2. DudoTheStampede

    DudoTheStampede

    Joined:
    Mar 16, 2015
    Posts:
    79
    If I assume correctly what you're trying to achieve, you should change your Trigger script in this way:

    Code (CSharp):
    1. void Awake ()
    2.     {
    3.         guiScript = guiObject.GetComponent<Question1> ();
    4.         guiScript.enabled = false; //Turn off the script, so the OnGUI function doesn't draw the question window
    5.     }
    6.     void OnTriggerEnter (Collider other)
    7.     {
    8.         guiScript.enabled = true; //Turn on the script (I think your line was just wrong)
    9.         Destroy (this.gameObject);
    10.     }
    Same modification for Trigger2 and it should be okay!
     
    RKM_91 likes this.
  3. RKM_91

    RKM_91

    Joined:
    Oct 27, 2013
    Posts:
    28
    Thank you very much. :)