Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
4 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by KobyWan17 · Oct 27, 2021 at 03:33 AM · instantiatebuttonsparametersspawning-enemiesinterval

Difficulty parameter not correctly affecting spawn rate on button click

I've been stuck on this problem for about a week and am at a loss of how to resolve it. I am trying to speed up the instantiate rate of the enemies when the difficulty buttons on the start screen are clicked. In order to do this I've set a public difficulty variable in my DifficultyButton script and set it in the inspector to 1-3 depending on the button, and I'm trying to divide the spawnIntervalEnemy float from my SpawnManager script by that variable inside my GameManager script in order to make them spawn at different rates when the corresponding button is clicked. There seems to be no errors, but all 3 buttons keep the enemies at the same spawn rate no matter which one I click. I'm having to pull variables from multiple different scripts so idk if that's what's causing the issue or what, and I've tried multiple different solutions but nothing seems to do the trick and make the buttons spawn enemies at the correct rate, so any help is appreciated. Here are my 3 scripts with the relevant code, hopefully this helps.

 public class DifficultyButton : MonoBehaviour
 {
     private Button button;
     private GameManager gameManager;
 
     public int difficulty;
 
     // Start is called before the first frame updateX
     void Start()
     {
         button = GetComponent<Button>();
         gameManager = GameObject.Find("Game Manager").GetComponent<GameManager>();
 
         button.onClick.AddListener(SetDifficulty);
     }
 
     // Update is called once per frame
     void Update()
     {
         
     }
 
     void SetDifficulty()
     {
         Debug.Log(gameObject.name + " was clicked");
         gameManager.StartGame(difficulty);
     }

-------------------------------------------------------------------------------------------------

 public class GameManager : MonoBehaviour
 {
     public TextMeshProUGUI scoreText;
     public TextMeshProUGUI gameOverText;
     public static bool isGameActive;
     public Button restartButton;
     public SpawnManager other;
     public GameObject titleScreen;
 
     private int score;
 
     // Start is called before the first frame updateX
     void Start()
     {
 
     }
 
     // Update is called once per frame
     void Update()
     {
         
     }
 
     // Updates Score
     public void UpdateScore(int scoreToAdd)
     {
         score += scoreToAdd;
         scoreText.text = "Score: " + score;
     }
 
     public void GameOver()
     {
         gameOverText.gameObject.SetActive(true);
         restartButton.gameObject.SetActive(true);
         isGameActive = false;
     }
 
     public void RestartGame()
     {
         SceneManager.LoadScene(SceneManager.GetActiveScene().name);
     }
 
     public void StartGame(int difficulty)
     {
         isGameActive = true;
         // Starts score at 0
         score = 0;
         UpdateScore(0);
         other.InvokeObjects();
         other.spawnIntervalEnemy /= difficulty;
 
         titleScreen.gameObject.SetActive(false);
     }

-------------------------------------------------------------------------------------------------

 public class SpawnManager : MonoBehaviour
 {
     public GameObject[] enemies;
     public GameObject[] obstacles;
     public GameObject[] powerup;
 
     private float spawnPosZ = 15;
     private float spawnRangeX = 10;
     private float startDelay = 2;
     public float spawnIntervalEnemy = 1.5f;
     private float spawnIntervalObstacle = 1.0f;
     private float spawnIntervalPowerup = 5.0f;
 
     // Start is called before the first frame updateX
     void Start()
     {
         InvokeObjects();
     }
 
     // Update is called once per frame
     void Update()
     {
         if (!GameManager.isGameActive)
         {
             CancelInvoke("SpawnRandomEnemy");
             CancelInvoke("SpawnRandomObstacle");
             CancelInvoke("SpawnPowerup");
             enabled = false;
         }
     }
 
     void SpawnRandomEnemy()
     {
         int enemyIndex = Random.Range(0, enemies.Length);
         Vector3 spawnPos = new Vector3(Random.Range(-spawnRangeX, spawnRangeX), 0.75f, spawnPosZ);
 
         Instantiate(enemies[enemyIndex], spawnPos, enemies[enemyIndex].transform.rotation);
     }
 
     void SpawnRandomObstacle()
     {
         int obstacleIndex = Random.Range(0, obstacles.Length);
         Vector3 spawnPos = new Vector3(Random.Range(-spawnRangeX, spawnRangeX), .5f, spawnPosZ);
 
         Instantiate(obstacles[obstacleIndex], spawnPos, obstacles[obstacleIndex].transform.rotation);
     }
 
     void SpawnPowerup()
     {
         int powerupIndex = Random.Range(0, powerup.Length);
         Vector3 spawnPos = new Vector3(Random.Range(-spawnRangeX, spawnRangeX), .75f, spawnPosZ);
 
         Instantiate(powerup[powerupIndex], spawnPos, powerup[powerupIndex].transform.rotation);
     }
 
     public void InvokeObjects()
     {
         InvokeRepeating("SpawnRandomEnemy", startDelay, spawnIntervalEnemy);
         InvokeRepeating("SpawnRandomObstacle", startDelay, spawnIntervalObstacle);
         InvokeRepeating("SpawnPowerup", startDelay, spawnIntervalPowerup);
     }
 }
 


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by LoricGarde · Oct 28, 2021 at 10:50 AM

Have you tried making "spawnIntervalEnemy" private, and changing it's value with a public void method? You could add a Debug.Log to check that you ACTUALLY call this method, then print the difficulty variable and the spawnIntervalEnemy after the division is made. Something like:

 public class SpawnManager : MonoBehaviour
  {
  private float spawnIntervalEnemy;
 
 private void Start()
 {
 spawnIntervalEnemy = 1.5f;
 }
 
 public void SetDifficulty(float difficulty)
 {
 spawnIntervalEnemy=spawnIntervalEnemy/difficulty;
 Debug.Log("difficulty: "+ difficulty +" new value: "+spawnIntervalEnemy);
 }
 
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KobyWan17 · Oct 28, 2021 at 08:11 PM 0
Share

Thank you for your response, but I'm not exactly sure what you mean as you're combining 3 different scripts see$$anonymous$$gly into one in your response. The spawnIntervalEnemy is from SpawnManager, the SetDifficulty void is in the DifficultyButton, and I need the difficulty to change in the StartGame method in my GameManager script so the difficulty is called at the button click at the start of the game. Sorry if my original post wasn't clear, hopefully this clears it up because I'd really like your input, but your comment is kinda confusing.

avatar image LoricGarde KobyWan17 · Oct 29, 2021 at 08:42 AM 0
Share

Hi, I'm not combining different scripts, just modifying the spawnmanager. The names of the methods can be the same, you can change them if you want. When you call "other.spawnIntervalEnemy /= difficulty;" in StartGame, you're modifying a public variable. What I'm suggesting is storing that variable as private and modifying it in a method.

avatar image KobyWan17 LoricGarde · Oct 30, 2021 at 06:57 AM 0
Share

I think I see what you're saying. Problem is I need the SetDifficulty method to be in the DifficultyButton script so it's called on button click, so it can't be in the SpawnManager. This is my fault for not providing the relevant info, my SpawnManager actually has significantly more code but I didn't want to flood the post with it, but for clarity's sake I have updated the SpawnManager script to hopefully clear up this confusion. Hopefully with this cleared up we can work towards a solution because I greatly value you taking the time to help me and want to make the most of your time and efforts.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

171 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

EventTrigger on button not recognizing instantsiated player 1 Answer

How to make multiple buttons 0 Answers

NullReferenceException problems. 0 Answers

Instantiate button on runtime 2 Answers

How can I pass a reference to an Instantiated object? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges