1
votes

I've been working on this for so long but unfortunately, I'm not progressing at all. I am creating a personality quiz in Unity wherein each question have choices where in depending on each choice, a certain variable is increasing in value. The quiz seeks to show the user's top 5 best fit patterns (which is the variable with the highest value) based on his personality which will be based upon his answers. The public voids are called depending on which choice/button was clicked through the on click in the inspector.

public class QuizScript : MonoBehaviour {

public int snake = 0;
public int centipede = 0;

public void Q2C1() //question 2 choice 1
{
    snake = snake + 1;
    Debug.Log("Snake = " + snake); //for me to see if it works
}

public void Q3C1() //question 3 choice 1
{
    centipede = centipede + 1;
    Debug.Log("Centipede = " + centipede);
}
}

There are 26 patterns in all but in the mean time, I just want to sort two variables which are the snake and the centipede. I have searched for tutorials about sorting variables and there are short and long methods. But either way, I am having a hard time understanding it and applying it in my code. In one method, it is required to use another public static void or static int to do the sorting, but what confuses me is how will I be able to call it inside another public void which will be the one called and done when the button is clicked, which is not possible after trying it in the code. (Also, public static void does not recognize the already declared variables inside the class which is a big problem.) I have thought of calling the static void separately in the on click of the button that can be seen in the inspector but maybe because it is a static void, I cannot find it in the options.

This is the code where I am unable to call static int SortByScore in the inspector.

static int SortByScore(int snake, int centipede)
{
    return centipede.CompareTo(snake);
}

Thank you so so much in advance for the help. :)

3
Also, I'm trying to place the value of outcome in a variable so I will be able to know which pattern I will output. For example, if snake is the best fit pattern, I should output its image and meaning. I'm trying to do this like this var best = GetHighestOutcome(); if (best="Snake") { SceneManager.LoadScene("tat_Snake"); } but it says that it cannot convert type string to outcome.Sam

3 Answers

2
votes

I would look into using some sort of Dictionary type in order to achieve this.

You want to have a list of values that are tied together so you need an appropriate data type in order to do that. In the solution I propose, we use an enum to make a strong type out of the outcomes in your quiz, tying them together by using a Dictionary (which is a key-value table) and you can then order these using LINQ to get the outcome.

public class QuizScript : MonoBehavior {
    public Dictionary<Outcome, int> Outcomes = new Dictionary<Outcome, int>() {
        { Outcome.Snake, 0 },
        { Outcome.Centipede, 0 }
    };

    public void Q2C1() {
        Outcomes[Outcome.Snake]++;

        Debug.Log("Snake: " + Outcomes[Outcome.Snake]);
    }

    public void Q3C1() {
        Outcomes[Outcome.Centipede]++;

        Debug.Log("Centipede: " + Outcomes[Outcome.Centipede]);
    }

    public Outcome GetHighestOutcome() {
        // Order by the 'key' of the dictionary, i.e. the number we tied to the outcome
        return Outcomes.OrderByDescending(choice => choice.Value).FirstOrDefault().Key;
    }
}

public enum Outcome {
    Snake,
    Centipede
}

At OP's additional request, you could do a sort and then get the top items like so:

public List<Outcome> GetHighestOutcome() {
    // Order by the 'key' of the dictionary, i.e. the number we tied to the outcome
    var maxValue = Outcomes.OrderByDescending(choice => choice.Value).FirstOrDefault().Value;
    var kvPairs = Outcomes.Find(kv => kv.Value == maxValue);
    return kvPairs.Select(kv => kv.Key).ToList();
}
1
votes

I think your strategy is getting in-your-way. This is a good example where you would be better-off storing your values in a HashSet or Dictionary.

This is how your code would look if you used a Dictionary instead:

public class QuizScript : MonoBehaviour {
  public System.Collections.Generic.Dictionary<string, int> characteristic = new Dictionary<string, int>();

  //public int snake = 0;
  //public int centipede = 0;

  //constructor initializes your set
  public QuizScript() {
        characteristic.Add("snake",0);
        characteristic.Add("centipede",0);          
  }

  public void Q2C1() //question 2 choice 1
  {
      characteristic["snake"] += 1;
      Debug.Log("Snake = " + characteristic["snake"].ToString()); //for me to see if it works
  }

  public void Q3C1() //question 3 choice 1
  {
      characteristic["centipede"] += 1;
      Debug.Log("Centipede = " + characteristic["centipede"].ToString());
  }
}
0
votes

What's your goal at the end of the project? Do you want to display the variables in order from biggest to smallest? If so, you could load all the variables into an array, and let the array do the sorting for you. The Array class has a Sort() function!

So, for example, you could have an int array of variables underneath your other variable declarations, then you could load the array with all your variables:

variableArray = {snake, centipede, ...} //the rest of your variables

Then, use the Array class' Sort function to sort it for you. Check out how to declare an array and how to use Array's Sort function, and you should be all set.

Good luck!