//Spawns 'newstars' amount of star objects at random x z positions with y = 0, and then rises them up to a random y position //random positions in range of declared bounds public void SpawnStars() { //creates new stars at random positions with 1st being a solution, and others not for (int i = 0; i < newstars; i++) { GameObject newstar = Instantiate(star); StarScript sScript = (StarScript)newstar.GetComponent("StarScript"); if (i == 0) { // keep track of the solution star solutionStar = newstar; // initialize star to be a solution sScript.isSolution = true; string val = (num1 * num2).ToString(); sScript.ChangeTextTo(val); } else { // keep track of non solutions nonSolutionStars.Add(newstar); // initialize star to be a non conflicting wrong answer sScript.isSolution = false; string val = this.CreateFalseSolution(i); sScript.ChangeTextTo(val); } setStarPos(sScript); } }
// Handles selecting a star public void SelectStar(GameObject selectedObj) { StarScript sScript = (StarScript)selectedObj.GetComponent("StarScript"); // Selected star is the solution if (sScript.isSolution) { // remove false answers foreach (GameObject star in nonSolutionStars) { Destroy(star); } nonSolutionStars = new List <GameObject>(); this.Retire(questionStar); completedStars.Add(selectedObj); // transition answer star to question star selectedObj.tag = "Untagged"; questionStar = selectedObj; sScript.isSolution = false; // increment number of correctly answered questions and check if game won answeredQuestions = answeredQuestions + 1; if (answeredQuestions == questionsPerGame) { this.GameEnd(true); this.Retire(questionStar); } //if game not one, create a new question and spawn new stars else { num1 = (int)Random.Range(multLowBound, multHighBound); num2 = (int)Random.Range(multLowBound, multHighBound); string newVal = num1.ToString() + " x " + num2.ToString(); sScript.ChangeTextTo(newVal); SpawnStars(); } } // selected star was not the correct answer else { // removese the star sScript.Remove(); this.nonSolutionStars.Remove(selectedObj); // increments the number of wrongly guessed stars wrongAnswers = wrongAnswers + 1; // end game if too many incorrect answers if (wrongAnswers == wrongAnswersPerGame) { this.GameEnd(false); } } }