Search Unity

Quiz Game scripting help

Discussion in 'Scripting' started by Rhys_Pieces66, May 1, 2017.

  1. Rhys_Pieces66

    Rhys_Pieces66

    Joined:
    May 2, 2014
    Posts:
    4
    Good day all,

    Seeking some assistance with what could be a simple problem but as a newcomer it is challenging me. I've followed unity quiz tutorial and now have met upon a problem, I have a screen that has multiple buttons each corresponding to a different round/set of questions. What i would like to do is have each button change the roundData information so different questions appear depending on the button selected. I've attached a pic of my selection screen with two buttons to show what i mean.
     

    Attached Files:

  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    You didn't really give any info on how you are creating your question data or storing it to begin with, so all I can say is you click one button and maybe change a static variable to indicate what data you want to load. Then when you go to the game scene, you check that value to see what set of question data you need to retrieve and load that up.
     
  3. Rhys_Pieces66

    Rhys_Pieces66

    Joined:
    May 2, 2014
    Posts:
    4
    Sorry as i said i followed the unity quiz tutorial which creates an array called roundData which stores the questions and answers and as such i have put in multiple sets of questions to match each round. eg. The roundData array has 2 sets of questions and depending on the value of allRoundData[] it sets up the preferred questions.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;


    public class DataController : MonoBehaviour
    {
    public static RoundData[] allRoundData;


    // Use this for initialization
    void Start ()
    {
    DontDestroyOnLoad (gameObject);

    SceneManager.LoadScene ("Menu");
    }

    public RoundData GetCurrentRoundData()
    {
    return allRoundData [0];
    }

    // Update is called once per frame
    void Update () {

    }

    }
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    https://forum.unity3d.com/threads/using-code-tags-properly.143875/#post-3030443
    Please use code tags when posting code.

    So, allRoundData is an array and each index is a round of questions. So, you have what you need already. Now they probably set it up so there are several rounds and the questions load as you go through the rounds. I'm not sure how the data is put into that array, but if you wanted you could have each index be a different location on your map. If you want several rounds, then you need to populate that array with different questions.

    Since allRoundData is static, you just need to change that data before you go into your quiz scene. So on the map scene if I click on one button, it populates that array with a set of questions. If I click on another, a different set is loaded in.
     
  5. Rhys_Pieces66

    Rhys_Pieces66

    Joined:
    May 2, 2014
    Posts:
    4
    First of all sorry for not using code tags.

    Ok ... not trying to be a total noob (which I am) I am gonna add to the growing list of people that wonder why programmers behave like coding is so simple as saying the sky is blue so now go figure out how the tress are green. Not being rude just saying. The logic doesnt defeat me its the actually coding that does. in my question i asked how would i get the variable of AllRoundData[] to reflect the index i wanted. So not sure how telling me to do the same thing i asked how to do, is helping me?
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    As I said, I don't know how your array is populated. I didn't do this tutorial.
    I mention code tags so you know, it helps us help you. It's not bashing you at all.

    I don't normally help with code directly. I find it's better to help with logic.

    But, if you populate your array, then you now have your data. I'll just go by the assumption you know what you need to do there. I'll also go by the assumption that you have it all figured out except you're not sure how to set an index of choice.

    So, in this case you have a script with a static array. It has a method with a return that will return the set of questions you need. While not my choice way of doing it, if we go off just what you are coding right now, you might do something like...

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class DataController : MonoBehaviour
    7. {
    8. public static RoundData[] allRoundData;
    9. public static int level;
    10.  
    11. public RoundData GetCurrentRoundData()//Call this to get the proper set of questions.
    12. {
    13. return allRoundData [level];
    14. }
    15. }
    16.  
    Now, again, this will hopefully just help you go in the right direction.

    Now, your buttons need to set the level number so that when you go to the game scene you can load the proper data.

    So a button script could simply take an index.

    Code (CSharp):
    1. public void StartGame(int index)
    2. {
    3.    DataController.level = index; //Set the index so you have what questions need loading
    4.    SceneManager.LoadScene ("GameScene"); //Load your scene where your game is
    5. }
    Again, this is a guideline, not exact code. I just threw it together based off what you had above.
     
  7. Rhys_Pieces66

    Rhys_Pieces66

    Joined:
    May 2, 2014
    Posts:
    4
    Thank you that does point me in a direction. Don't mean to be defensive and I appreciate the assistance its just that many times person instead of pointing in a direction say simply do this or that, and if we as beginners knew to do this or that we would not be beginners. Again I really appreciate the time you are taking to help me here and hope I did not come off as a jerk ;).
     
  8. cystus

    cystus

    Joined:
    Mar 20, 2018
    Posts:
    8
    hello Brathnann, Rhys_Pieces66, were u able to resolve d issue. pls assist with ur solution bcos I tried the codes above but keep getting Null reference error. tanx
     
  9. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You should create a new thread of your own with your question and explain what is working and/or not working. Include any code, and error messages that may be relevant. :)
     
    cystus likes this.
  10. cystus

    cystus

    Joined:
    Mar 20, 2018
    Posts:
    8

    tanx for ur response.
    I replied on this thread bcos I have the same issue and used the solution provided here.
    the error is; NullReferenceException: Object reference not set to an instance of an object


    I have d datacontroller on the persistent scene, level buttons on Level scene and the game controller on the game scene.

    bellow are my codes.

    using System.Collections;
    using UnityEngine;
    using UnityEngine.SceneManagement;

    public class LevelScreen : MonoBehaviour


    public void StartGame(int index)
    {
    DataController.level = 1; //Loads level 1
    SceneManager.LoadScene ("GameScene"); //Load my game scene
    }









    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;

    public class DataController : MonoBehaviour


    {
    public static RoundData[] allRoundData;
    public static int level;
    // Use this for initialization
    void Start ()
    {
    DontDestroyOnLoad (gameObject);

    SceneManager.LoadScene ("Menu");
    }

    public RoundData GetCurrentRoundData()//Call this to get the proper set of questions.
    {
    return allRoundData [level];
    }
    }


    The error points to this line in the last script; return allRoundData [level];
     
  11. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Have you initialized the array and set its contents (allRoundData)?
     
  12. cystus

    cystus

    Joined:
    Mar 20, 2018
    Posts:
    8
    Yes

    using UnityEngine;

    using System.Collections;


    [System.Serializable]

    public class RoundData

    {

    public string name;

    public int timeLimitInSeconds;

    public int pointsAddedForCorrectAnswer;

    public QuestionData[] questions;


    }
     
  13. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
  14. cystus

    cystus

    Joined:
    Mar 20, 2018
    Posts:
    8
    I am a newbie to C#, kindly point me in the right direction with codes if necessary.
    I'm just trying my hand on the quiz game for the first time.
     
  15. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Can you send me the link to where you saw how to make the DataController script? :)
     
  16. cystus

    cystus

    Joined:
    Mar 20, 2018
    Posts:
    8
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class DataController : MonoBehaviour
    7. {
    8.  
    9.     // Use this for initialization
    10.     void Start ()
    11.     {
    12.         DontDestroyOnLoad (gameObject);
    13.  
    14.         SceneManager.LoadScene ("MenuScreen");
    15.     }
    16.  
    17. public static RoundData[] allRoundData;
    18. public static int level;
    19.  
    20. public RoundData GetCurrentRoundData()//Call this to get the proper set of questions.
    21. {
    22. return allRoundData [level];
    23. }
    24. }
    25.  
     
  17. cystus

    cystus

    Joined:
    Mar 20, 2018
    Posts:
    8
    dats d DataController Script and Below is the Script for the button on Level scene that loads a level.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class DataController : MonoBehaviour
    7.  
    8.  
    9. public void Btn1 (int index)
    10. {
    11.    DataController.level = 0;
    12.    SceneManager.LoadScene ("Game");
    13. }
    14.  
     
  18. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    So, something on this line is null:
    Code (csharp):
    1. public static RoundData[] allRoundData;
    How do you fill in that array?
     
  19. cystus

    cystus

    Joined:
    Mar 20, 2018
    Posts:
    8
    The last script I sent fills d array with the required index which is zero in this case.
     
  20. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Sorry, that's not right...Setting a variable that acts as an index does not equate to creating an array or filling it.

    You're not sure what I'm talking about.. :(

    I do not know if you followed a different tutorial or a more progressed version to the one I quickly looked up here to assist you, but in the video I saw they did not use a static variable. They filled the data in inside the inspector (and later from JSON, I believe.. stopped paying too much attention later on).

    So, you could experiment by removing the 'static' from the RoundData variable... Then, look in the inspector of the game object with that script and fill in some values.

    If this is very new to you, perhaps you want to give yourself some introduction to C#: https://unity3d.com/learn/tutorials/s/scripting
     
    cystus likes this.
  21. cystus

    cystus

    Joined:
    Mar 20, 2018
    Posts:
    8
    alright, tanx a lot. I'll go through the tutorials.
     
  22. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I think that will be good for you in general.. Work your way up so you have a better understanding of the next tutorial you see, and the one after that, and so on. Practice what you learn, too, and you will absorb it much better! :) Have fun.
     
    cystus likes this.