Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by xyHeat · Jun 01, 2016 at 05:08 PM · c#enemyspawnswitch

Switch() problem [C#]

Hey everybody ! I'm making a game with an enemy spawner and a GameManager who work with a Switch statement. I would like to disable my player and my enemy spawner in "case 2:" but my spawner and my player aren't disabled ... this is my script :

     void StateMachine()
     {
         switch (State)
         {
             case 0: // INTRO
                 endCanvas.SetActive(false);
                 playerShip.SetActive(true);
                 break;
             case 1: // GAME
                 mobSpawner.GetComponent<MobSpawner>().enabled = true;
                 playerShip.SetActive(true);
                 break;
             case 2: // END GAME
                 Debug.Log("End game");
                 mobSpawner.GetComponent<MobSpawner>().enabled = false;
                 Debug.Log("MobSpawner disabled");
                 playerShip.SetActive(false);
                 Debug.Log("PlayerShip disabled");
                 endCanvas.SetActive(true);
                 text_Kills.text = "You killed " + kills + " enemies !";
                 break;
         }

The canvas appear, the console say : "End Game"

"MobSpawner disabled"

"PlayerShip disabled"

Someone can help me ?

Thank you,

Bye, xyHeat

EDIT :

the entire script :

 public class GameManager : MonoBehaviour {
 
     public GameObject playerShip;
     public GameObject mobSpawner;
     public GameObject endCanvas;
 
     public Text text_Kills;
 
     public int State;
     public int kills;
 
     void Start()
     {
         State = 1;
         StateMachine();
     }
 
     void StateMachine()
     {
         switch (State)
         {
             case 0: // INTRO
                 endCanvas.SetActive(false);
                 playerShip.SetActive(true);
                 break;
             case 1: // GAME
                 mobSpawner.GetComponent<MobSpawner>().enabled = true;
                 playerShip.SetActive(true);
                 break;
             case 2: // END GAME
                 Debug.Log("End game");
                 mobSpawner.GetComponent<MobSpawner>().enabled = false;
                 Debug.Log("MobSpawner disabled");
                 playerShip.SetActive(false);
                 Debug.Log("PlayerShip disabled");
                 endCanvas.SetActive(true);
                 text_Kills.text = "You killed " + kills + " enemies !";
                 break;
         }
     }
 
     public void ToIntro()
     {
         State = 0;
         StateMachine();
     }
 
     public void ToGame()
     {
         State = 1;
         StateMachine();
     }
 
     public void ToEndGame()
     {
         State = 2;
         StateMachine();
     }
 }
 
Comment
Add comment · Show 8
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 TreyH · Jun 01, 2016 at 05:31 PM 0
Share

If you attach print statements to the other cases, do they print after the case2 debug lines? You might be reactivating them somewhere?

avatar image xyHeat TreyH · Jun 01, 2016 at 05:40 PM 0
Share

The others debugs are print. The active the case 2, i use that : public void ToEndGame() { State = 2; State$$anonymous$$achine(); }

who is called when the player get 0 lives

avatar image TreyH xyHeat · Jun 01, 2016 at 05:46 PM 0
Share

The ones from case2 print, but if you put debug.print statements in case1, do they fire after case2?

Show more comments
Show more comments
avatar image xyHeat TreyH · Jun 01, 2016 at 05:50 PM 0
Share

Yes, they are enabled

avatar image Habitablaba · Jun 01, 2016 at 06:01 PM 0
Share

Have you verified that the object you expect to be the player is the actual object you're giving your script in the playerShip variable?

avatar image xyHeat Habitablaba · Jun 01, 2016 at 06:04 PM 0
Share

Yes, player isn't disabled and enemies still spawn...

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Raresh · Jun 01, 2016 at 06:15 PM

Make sure there aren't any objects calling your methods. From your code, a way to quickly determine that would be to look at the endCanvas. If that gets enabled, but the others stay disabled, it means that something else is re-enabling your scripts.

Also i would make the StateMachine method have an int or enum as a parameter, so you don't have a global variable. Just call StateMachine (2) or if you use enums StateMachine(GameStates.Intro) or something similar.

Comment
Add comment · Show 4 · 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 xyHeat · Jun 01, 2016 at 06:37 PM 0
Share

Hi! I follow your instruction and i make that :

     void Start()
     {
         State$$anonymous$$achine(1);
     }
 
     void State$$anonymous$$achine(int State)
     {
         switch (State)
         {
             case 0: // INTRO
                 endCanvas.SetActive(false);
                 playerShip.SetActive(true);
                 break;
             case 1: // GA$$anonymous$$E
                 mobSpawner.GetComponent<$$anonymous$$obSpawner>().enabled = true;
                 playerShip.SetActive(true);
                 break;
             case 2: // END GA$$anonymous$$E
                 Debug.Log("End game");
                 mobSpawner.GetComponent<$$anonymous$$obSpawner>().enabled = false;
                 Debug.Log("$$anonymous$$obSpawner disabled");
                 playerShip.SetActive(false);
                 Debug.Log("PlayerShip disabled");
                 endCanvas.SetActive(true);
                 text_$$anonymous$$ills.text = "You killed " + kills + " enemies !";
                 break;
         }
     }
 
     public void ToIntro()
     {
         State$$anonymous$$achine(0);
     }
 
     public void ToGame()
     {
         State$$anonymous$$achine(1);
     }
 
     public void ToEndGame()
     {
         State$$anonymous$$achine(2);
     }
 }
 

then the canvas appear, the player disapear but the enemy still spawn.

I check if something call it, but nothing do it...

This is my spawn script : public GameObject enemy; public GameObject enemy2;

 float maxSpawnRateInSeconds = 5f;


 void Start()
 {
     Invoke("SpawnEnemy", maxSpawnRateInSeconds);

     InvokeRepeating("IncreaseSpawnRate", 0f, 15f);
 }

 void SpawnEnemy()
 {

     Vector2 $$anonymous$$ = Camera.main.ViewportToWorldPoint(new Vector2(0, 0));

     Vector2 max = Camera.main.ViewportToWorldPoint(new Vector2(1, 1));


     float i = Random.Range(1.0f, 2.0f);
     if(i <= 1.5f)
     {
         GameObject anEnemy = (GameObject)Instantiate(enemy);
         anEnemy.transform.position = new Vector2(Random.Range($$anonymous$$.x, max.x), max.y);
     } else
     {
         GameObject anEnemy = (GameObject)Instantiate(enemy2);
         anEnemy.transform.position = new Vector2(Random.Range($$anonymous$$.x, max.x), max.y);
     }
     ScheduleNextEnemySpawn();
 }

 void ScheduleNextEnemySpawn()
 {
     float spawnInNSeconds;

     if (maxSpawnRateInSeconds > 1f)
     {
         spawnInNSeconds = Random.Range(1f, maxSpawnRateInSeconds);
     }
     else
     {
         spawnInNSeconds = 1f;
     }
     Invoke("SpawnEnemy", spawnInNSeconds);

 }

 void IncreaseSpawnRate()
 {
     if (maxSpawnRateInSeconds > 1f)
     {
         maxSpawnRateInSeconds--;
     }
     if (maxSpawnRateInSeconds == 1f)
     {
         CancelInvoke("IncreaseSpawnRate");
     }
 }
avatar image Raresh xyHeat · Jun 01, 2016 at 07:05 PM 1
Share

See now it makes sense. You schedule to spawn a new wave but aren't actually checking when spawning the wave if the game ended.

 void SpawnEnemy()
 {
     if( State!=2)
    {
       // do stuff
    }
 
 }

You still need to have a public variable for the game state, and you can set it's value in

 public int State;
 
 State$$anonymous$$achine(int State)
 {
    this.State = State;
 }

avatar image troien xyHeat · Jun 02, 2016 at 10:14 AM 1
Share

I think @Raresh is right in his last comment, and his solution should work. But in this comment I intent to explain more on the why your code doesn't stop.

Are you sure that your spawner isn't disabled? (in the inspector the checkbox next to the scripts name) Because reading this answer, you can read that Invoke and InvokeRepeating don't actually care about whether you object is enabled or not and will hapilly continue to execute your code even when disabled.

In either way, I think using coroutines for this is a better option for performance if you care about that :p. Stopping your code from being executed can either be done by polling your State (or this.enabled) in the SpawnEnemy method as explained above, or by calling CancelInvoke()/StopAllCoroutines() (depending on whether you use Invoke or coroutines) in the OnDisable of your spawn script.

avatar image xyHeat troien · Jun 02, 2016 at 03:26 PM 0
Share

Thank you, it work ! I didn't know that Invoke and InvokeRepeating weren't affected by disable or enable an object ! I'll look for the coroutines, I never pay attention to them.

Thank you again ! Bye, xyHeat

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

154 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

Related Questions

How to make my enemies spawn at spawn point, but not randomly? 1 Answer

Unity how to make different enemy spawn one at a time 1 Answer

How to spawn enemy? (Enemies) 1 Answer

Why does only one object respond, while more objects have exact same script? 0 Answers

Wave Spawner not working |Does not detect if an object is destroyed 1 Answer


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