Record of problems encountered in game production

Function: Get different results according to the player's choice

Ideas:

Use the PlayerPrefs class (a class for local persistent storage and reading) to record the variables yes, no, the number of questions q and the number of questions c (the initialization of several variables is written in the start method).

Write a choiceClick class to monitor the button behavior, where the yesClick() method records the number of clicks on the preferred option, and the method noClick() records the number of clicks on the second option. The number of problems during the game is set to a limited number (five), and when the fifth option is selected, jump to the final() method to compare the values ​​of the two buttons to achieve the scene of jumping to different results. (Aside: emmm there should be an easier way, ,)

Approximate code:

public void yesClick()
    {
        if (PlayerPrefs.GetInt("q") < c)//c is the number of questions
        {
            PlayerPrefs.SetInt("yes", PlayerPrefs.GetInt("yes")+1);//Update the value of y
            //Debug.Log("yes");
            if (PlayerPrefs.GetInt("q")==0)
            {
                //action 1
              }
            if(PlayerPrefs.GetInt("q") == 1)
            {
                //action 2
            }
            PlayerPrefs.SetInt("q",++i);
        }
        else
        {
            final();
        }
    }

Problem: The value of q does not change when the third selection is made in the same scene, it is still 0

Reason: The choiceClick class is added to the button, which is equivalent to the second selection and the third selection (the selection in this scene starts from the second one) are called independently, not the same one, so the value will only be repeated It only updates when the button is clicked.

Solution: remove the class, create an object named script and pull it into the prefab as a prefab, place the class into the object, and then pull it into onclick() to associate the method in the class with the button.


After the project is integrated, the value of q is not superimposed when the scene is switched, but the recording is restarted like the above problem

Reason: The start() method will run once every time the scene is switched, which means that these variables will be reinitialized every time the scene is switched.

Solution:

Create a new class in the first scene to initialize these variables, which means that these variables are only initialized once throughout the game



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324510732&siteId=291194637
Recommended