Search Unity

Help please!!

Discussion in 'Scripting' started by RKM_91, Mar 31, 2015.

  1. RKM_91

    RKM_91

    Joined:
    Oct 27, 2013
    Posts:
    28
    Hi,

    I'm trying to assign one question to one cube. I have 10 questions and 10 cubes with the numbers 1-10
    on them. Is there away to assign one question to one cube.


    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.Collections;
    5. using UnityEngine.UI;
    6.  
    7. public class GUIScreen : MonoBehaviour
    8. {
    9.     public bool Ebola;
    10.     public string Info;
    11.  
    12.     private List<string[]> questions  = new List<string[]>();
    13.     private List<int> answerOrder = new List<int>(new int[] {1,2,3,4});
    14.  
    15.     private int count;
    16.     public Text countText;
    17.  
    18.  
    19.    
    20.     void DrawInfo() {
    21.         Rect rect = new Rect(500, 100, 400, 200);
    22.         Rect close = new Rect(600, 500, 200, 100);
    23.         if(GUI.Button(close, "CLICK")) {
    24.             Ebola = !Ebola;
    25.         }
    26.        
    27.         if (Ebola) {
    28.             GUI.Box(rect, Info);
    29.             GUI.Label(new Rect(520, 110, 400, 30), questions[0][0]);
    30.             if (GUI.Button(new Rect(520, 200, 100, 30), questions[0][answerOrder[0]])) {
    31.                 HandleAnswer(answerOrder[0]);
    32.             }
    33.             if (GUI.Button(new Rect(520, 250, 100, 30), questions[0][answerOrder[1]])) {
    34.                 HandleAnswer(answerOrder[1]);
    35.            
    36.             }
    37.             if (GUI.Button(new Rect(780, 200, 100, 30), questions[0][answerOrder[2]])) {
    38.                 HandleAnswer(answerOrder[2]);
    39.            
    40.             }
    41.             if (GUI.Button(new Rect(780, 250, 100, 30), questions[0][answerOrder[3]])) {
    42.  
    43.                 HandleAnswer(answerOrder[3]);
    44.            
    45.             }
    46.        
    47.         }
    48.    
    49.     }
    50.    
    51.     private void HandleAnswer(int answer) {
    52.         if (answer == 1)
    53.         {
    54.             count = count + 1;
    55.             SetCountText ();
    56.             //Destroy (this.gameObject);
    57.  
    58.             //handle correct answer
    59.             NextQuestion();
    60.  
    61.  
    62.         }
    63.         else {
    64.             //Handle wrong answer
    65.             Destroy (this.gameObject);
    66.  
    67.        
    68.         }
    69.     }
    70.  
    71.     void SetCountText ()
    72.     {
    73.         countText.text = "Score: " + count.ToString ();
    74.     }
    75.  
    76.    
    77.     void OnGUI() {
    78.         if(questions.Count > 0) {
    79.             DrawInfo();
    80.         }
    81.     }
    82.    
    83.     void Start() {
    84.  
    85.         count = 0;
    86.         SetCountText ();
    87.  
    88.         // String order: question, correct, wrong, wrong, wrong
    89.         questions.Add(new string[] { "What year did Ebola begin?", "1976", "1986", "1996", "1966" });// Question 1
    90.         questions.Add(new string[] { "What is the Ebola virus named after?", "A river", " A farm", "A tree", "A city" }); // Question 2
    91.         questions.Add(new string[] { "Which of these can pass along the ebola virus?", "Tears", "Dandruff", "Fingernails", "Soap" }); // Question 3
    92.         questions.Add(new string[] { "Ebola is contagious only when someone has symptoms?", "True", "False", "", "" }); // Question 4
    93.         questions.Add(new string[] { "Scientists think Ebola first came from?", "Bats", "Dogs", "Gorillas", "Mosquitoes" }); // Question 5
    94.         questions.Add(new string[] { "Men who recover from Ebola should do this for 3 months?", "Wear condoms", "Test blood sugar", "Use private toilets", "Shower twice a day" }); // Question 6
    95.         questions.Add(new string[] { "Which is the bigger threat: Ebola or the flu?", "Flu", "Ebola", "", "" }); // Question 7
    96.         questions.Add(new string[] { "How many strains of the Ebola virus are there?", "Five", "One", "Three", "More than 100" }); // Question 8
    97.         questions.Add(new string[] { "How long can the Ebola virus live on something outside the body?", "Up to 6 days", "Up to 6 hours", "Up to 6 minutes", "Up to 6 months" }); // Question 9
    98.         questions.Add(new string[] { "How many proven treatments are there for Ebola?", "None", "One", "Two", "Three" }); // Question 10
    99.  
    100.         Shuffle(questions);
    101.         Shuffle(answerOrder);
    102.     }
    103.  
    104.     public void RestartLevel ()
    105.     {
    106.         Application.LoadLevel (Application.loadedLevel);
    107.     }
    108.    
    109.     void NextQuestion() {
    110.         questions.RemoveAt(0);
    111.         Shuffle(answerOrder);
    112.     }
    113.  
    114.     static readonly System.Random rng = new System.Random();
    115.     public static void Shuffle<T>(IList<T> list) {
    116.         int n = list.Count;
    117.         while(n > 1) {
    118.             n--;
    119.             int k = rng.Next(n + 1);
    120.             T value = list[k];
    121.             list[k] = list[n];
    122.             list[n] = value;
    123.         }
    124.     }
    125. }

     
  2. RKM_91

    RKM_91

    Joined:
    Oct 27, 2013
    Posts:
    28
    Had a search online & watched videos but can't seem to solve this problem?

    Code (CSharp):
    1. private void HandleAnswer(int answer) {
    2.         if (answer == 1)
    3.         {
    4.             count = count + 1;
    5.             SetCountText ();
    6.             Destroy (this.gameObject);
    7.  
    8.             //handle correct answer
    9.             NextQuestion();
    10.  
    11.         }
    12.         else {
    13.             //Handle wrong answer
    14.             Destroy (this.gameObject);
    15.    
    16.         }
    17.     }


    So at the moment player walks into cube, cube gets destroyed, UI box appears on screen with a question, answer question correctly, UI box disappears, 1 is added to score.

    If answer is incorrectly, UI box disappears, score remains at 0.

    How do i repeat this for all 10 cubes?

    Anyone have any ideas or tutorials, videos, etc on this?
     
  3. RKM_91

    RKM_91

    Joined:
    Oct 27, 2013
    Posts:
    28
    Bump.
     
  4. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Create a Question monobehaviour with all the questaion data. Add this to each cube and set the different question data per cube. Then in your collision function with a cube, use a GetComponent<Question>() to access that data before you destroy it.
     
  5. RKM_91

    RKM_91

    Joined:
    Oct 27, 2013
    Posts:
    28
    Thanks for the reply, I'm very new to unity & programming c# so I'm not sure what you mean so will try and clarify things with you.

    So I made a new script called Question and have assigned the question data to the script.
    Will I need to comment out the questions for each cube i will assign the script too? So for example if I comment out question 1 and place the script on cube number 2 would this work?

    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.Collections;
    5. using UnityEngine.UI;
    6.  
    7. public class Question : MonoBehaviour
    8. {
    9.  
    10.     public bool Ebola;
    11.     public string Info;
    12.  
    13.     private List<string[]> questions  = new List<string[]>();
    14.     private List<int> answerOrder = new List<int>(new int[] {1,2,3,4});
    15.  
    16.  
    17.     void Start() {
    18.  
    19.         // String order: question, correct, wrong, wrong, wrong
    20.         //questions.Add(new string[] { "What year did Ebola begin?", "1976", "1986", "1996", "1966" });// Question 1
    21.         questions.Add(new string[] { "What is the Ebola virus named after?", "A river", " A farm", "A tree", "A city" }); // Question 2
    22.         questions.Add(new string[] { "Which of these can pass along the ebola virus?", "Tears", "Dandruff", "Fingernails", "Soap" }); // Question 3
    23.         questions.Add(new string[] { "Ebola is contagious only when someone has symptoms?", "True", "False", "", "" }); // Question 4
    24.         questions.Add(new string[] { "Scientists think Ebola first came from?", "Bats", "Dogs", "Gorillas", "Mosquitoes" }); // Question 5
    25.         questions.Add(new string[] { "Men who recover from Ebola should do this for 3 months?", "Wear condoms", "Test blood sugar", "Use private toilets", "Shower twice a day" }); // Question 6
    26.         questions.Add(new string[] { "Which is the bigger threat: Ebola or the flu?", "Flu", "Ebola", "", "" }); // Question 7
    27.         questions.Add(new string[] { "How many strains of the Ebola virus are there?", "Five", "One", "Three", "More than 100" }); // Question 8
    28.         questions.Add(new string[] { "How long can the Ebola virus live on something outside the body?", "Up to 6 days", "Up to 6 hours", "Up to 6 minutes", "Up to 6 months" }); // Question 9
    29.         questions.Add(new string[] { "How many proven treatments are there for Ebola?", "None", "One", "Two", "Three" }); // Question 10
    30.      
    31.         Shuffle(questions);
    32.         Shuffle(answerOrder);
    33.     }
    34.  
    35.  
    36.     void NextQuestion() {
    37.         questions.RemoveAt(0);
    38.         Shuffle(answerOrder);
    39.     }
    40.  
    41.     static readonly System.Random rng = new System.Random();
    42.     public static void Shuffle<T>(IList<T> list) {
    43.         int n = list.Count;
    44.         while(n > 1) {
    45.             n--;
    46.             int k = rng.Next(n + 1);
    47.             T value = list[k];
    48.             list[k] = list[n];
    49.             list[n] = value;
    50.         }
    51.     }
    52. }
     
  6. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    No no, your new Question script can be seen as a container to hold individual Question data. i.e.

    Code (CSharp):
    1. public class Question : Monobehaviour
    2. {
    3.   public string m_Question;
    4.   public string[] m_multipleAnswers;
    5.   public int m_correctAnswerIndex;
    6.  
    7. }
    So if you add that on each cube, you set the question data via the inspector. This allows you to not have to hardcode all the questions in 1 file. And you also then don't need a list.
     
  7. RKM_91

    RKM_91

    Joined:
    Oct 27, 2013
    Posts:
    28
    Thanks for the reply,

    Sorry, could you show me an example using one of my questions because I'm not getting it.

    I can see in the inspector I will need to fill the relevant space in but should I create a new game object and assign it to the box or make a reference to the Question script in the trigger script which destroys the cubes?

     
  8. RKM_91

    RKM_91

    Joined:
    Oct 27, 2013
    Posts:
    28
    Bump :)
     
  9. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    Create yor cubes and place them down then attach the question script to each one filling out your questions and answer.

    Then depending on how you want the game to play effects how you handle the question change.

    Method 1: Add a public gameobject to the question script and set it to the next cube/question. Once the questioned is answered correctly set the camera focus to the next object. You will also need a bool for stating that the current cube/question is the active one.

    Method 2: Have a quiz script the holds a reference to each of the gameobjects and an int for deciding which one is active. You can then lerp between the cube/questions.

    You can do this in other ways but this is how i would go about it. Hope it helps.
     
  10. EgorHL570

    EgorHL570

    Joined:
    Apr 2, 2015
    Posts:
    2
    Всем привет! Помогите пожалуйста! Нашел скрипт в интернете, с помощью которого можно брать предметы как в Half Life 2, но он выдает ошибки:
    1 error CS0619: `UnityEngine.Component.camera' is obsolete: `Property camera has been deprecated. Use GetComponent<Camera>() instead. (UnityUpgradable)'

    2
    UnityEngine.Component' does not contain a definition for `ScreenPointToRay' and no extension method `ScreenPointToRay' of type `UnityEngine.Component' could be found (are you missing a using directive or an assembly reference?)
    У меня Unitu5. Помогите пожалуйста исправить скрипт или найти похожий.