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.
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.
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 () { } }
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.
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?
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): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class DataController : MonoBehaviour { public static RoundData[] allRoundData; public static int level; public RoundData GetCurrentRoundData()//Call this to get the proper set of questions. { return allRoundData [level]; } } 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): public void StartGame(int index) { DataController.level = index; //Set the index so you have what questions need loading SceneManager.LoadScene ("GameScene"); //Load your scene where your game is } Again, this is a guideline, not exact code. I just threw it together based off what you had above.
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 .
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
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.
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];
Yes using UnityEngine; using System.Collections; [System.Serializable] public class RoundData { public string name; public int timeLimitInSeconds; public int pointsAddedForCorrectAnswer; public QuestionData[] questions; }
that's the class, but that's not what I asked. Did you initialize the array and add instances of this class to it? For future reference, please read this page on how to post code nicely on the forums: https://forum.unity.com/threads/using-code-tags-properly.143875/
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.
Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class DataController : MonoBehaviour { // Use this for initialization void Start () { DontDestroyOnLoad (gameObject); SceneManager.LoadScene ("MenuScreen"); } public static RoundData[] allRoundData; public static int level; public RoundData GetCurrentRoundData()//Call this to get the proper set of questions. { return allRoundData [level]; } }
dats d DataController Script and Below is the Script for the button on Level scene that loads a level. Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class DataController : MonoBehaviour public void Btn1 (int index) { DataController.level = 0; SceneManager.LoadScene ("Game"); }
So, something on this line is null: Code (csharp): public static RoundData[] allRoundData; How do you fill in that array?
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
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.