2021年4月8日星期四

Difficulty settings carryover to game scenes via playerprefs

Thx to some excellent suggstions, I have Difficulty buttons changing spawn rate for my game. However, the changes aren't translating to subsequent scenes. Can anyone spot the issue with my Playerprefs settings?

Difficulty buttons script

private Button button;

public RandomSpawn randomSpawn;    public int difficulty;      void Start()  {      button = GetComponent<Button>();       randomSpawn = GameObject.Find("Random Spawn").GetComponent<RandomSpawn>();      button.onClick.AddListener(delegate{randomSpawn.UpdateSpawnRate(difficulty);});  }    void SetDifficulty()  {      Debug.Log(gameObject.name + "was clicked");  }     void Update()  {      PlayerPrefs.SetInt("Player Difficulty", difficulty);  }  

}

Spawn script

public GameObject prefab1, prefab2, prefab3, prefab4;

public float maxSpawnRate = 2f;    private float nextSpawn = 0f;    private int whatToSpawn;    private float spawnRate = 2f;    public static int difficulty = 0;    private void Start()  {      difficulty = PlayerPrefs.GetInt("Player Difficulty");  }    void Update()  {            if (Time.time > nextSpawn) { //if time has come          whatToSpawn = Random.Range(1, 6); // define random value between 1 and 4 (5 is exclusive)          Debug.Log(whatToSpawn); //display its value in console                    switch (whatToSpawn) {              case 1:                  Instantiate(prefab1, transform.position, Quaternion.identity);                  break;              case 2:                  Instantiate(prefab2, transform.position, Quaternion.identity);                  break;              case 3:                  Instantiate(prefab3, transform.position, Quaternion.identity);                  break;              case 4:                  Instantiate(prefab4, transform.position, Quaternion.identity);                  break;                   }             nextSpawn = Time.time + spawnRate;      }  }  
https://stackoverflow.com/questions/67013294/difficulty-settings-carryover-to-game-scenes-via-playerprefs April 09, 2021 at 07:53AM

没有评论:

发表评论