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 tatamark84 · Jul 27, 2018 at 11:55 PM · gameoverendgame

SpaceShooter Win the game!

I expand a little Space Shooter. When the player arrives the score "100", wins the game. when winning the game, I want to Destroy all "Enemy" tag immediately, but the game continues until all hazards come down. and maybe hit the player. Can you help me?

link text

gamecontroller-win.txt (379 B)
Comment
Add comment · Show 1
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 toddisarockstar · Jul 28, 2018 at 01:24 AM 0
Share

first of all to find where your problem is try putting a a print statement in the WinGame function to see if the code is even going there.

 public void WinGame()
     {
 print("if i see this in the console i know that this function is even running!!!");
         GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
         for (int i = 0; i < enemies.Length; i++)
          {
            Destroy(enemies[i]);
          }
         win = true;
         winText.text = "You Win!!!!!";
         gameOver = true;
     }

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by PolyvivalStudios · Jul 28, 2018 at 03:20 PM

I'm not an expert, but you could try to replace the for loop with a foreach loop. @tatamark84 Also, Please Post The Full Code :)

Example:

foreach(GameObject enemy in enemies) { destroy(enemy); }

Comment
Add comment · 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
0

Answer by MT369MT · Jul 28, 2018 at 03:48 PM

Hi you could use:

 if (score >= 100)
 {
     GameObject[] Enemies = GameObject.FindGameObjectsWithTag("Enemy");
     foreach(GameObject Enemy in Enemies) 
     {
         destroy(Enemy);
     }
 }

Comment
Add comment · 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
0

Answer by tatamark84 · Jul 30, 2018 at 05:16 PM

Actually, when winning the game, some enemies continue coming. I click on enemies, then all of them destroyed. But new enemies come again until they finished. I don't want this happened. I want to destroy all of the enemies forever.

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement;

 public class GameController : MonoBehaviour
 {
     public GameObject[] hazards;
     public Vector3 spawnValues;
     public int hazardCount;
     public float spawnWait;
     public float startWait;
     public float waveWait;
 
     public Text scoreText;
     public Text restartText;
     public Text gameOverText;
     public Text levelText;
     public Text winText;
 
     public ParticleSystem ps;
 
     private int score;
     private bool gameOver;
     private bool restart;
     private bool win;
     private int level = 1;
 
 
     void Start()
     {
         gameOver = false;
         restart = false;
         win = false;
         restartText.text = "";
         gameOverText.text = "";
         levelText.text = "Level " + level;
         winText.text = "";
         score = 0;
         UpdateScore();
         StartCoroutine(SpawnWaves());
         Screen.SetResolution(600, 900, true);
     }
 
     void Update()
     {
         if (restart)
         {
             if (Input.GetKeyDown(KeyCode.R))
             {
                 SceneManager.LoadScene("Main");
             }
         }
         if (Input.GetKeyDown(KeyCode.Escape))
         { Application.Quit(); }
     }
 
 
 
     IEnumerator SpawnWaves()
     {
         yield return new WaitForSeconds(startWait);
 
         while (true)
         {
            
                 for (int i = 0; i < hazardCount; i++)
             {
                 GameObject hazard = hazards[Random.Range(0, hazards.Length)];
                 Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
                 Quaternion spawnRotation = Quaternion.identity;
                 Instantiate(hazard, spawnPosition, spawnRotation);
                 yield return new WaitForSeconds(spawnWait);
 
             }
             yield return new WaitForSeconds(waveWait);
 
             if (gameOver)
             {
                 restartText.text = "Press 'R' for Restart or Esc to Exit!";
                 restart = true;
                 break;
             }
 
         }
         
     }
 
     public void AddScore(int newScoreValue)
     {
         score += newScoreValue;
         UpdateScore();
 }
      
         if (score >= 100)
         {
            
             WinGame();
            
         }
     }
        
     void UpdateScore()
     {
         scoreText.text = "Score: " + score;
     }
 
     public void GameOver()
     {
         gameOverText.text = "Game Over!";
         gameOver = true;
     }
 
     public void WinGame()
     {
         GameObject[] Enemies = GameObject.FindGameObjectsWithTag("Enemy");
        foreach(GameObject Enemy in Enemies)
         {
             Destroy(Enemy);
         }
         win = true;
         winText.text = "You Win!!!!!";
         gameOver = true;
     }
 }
Comment
Add comment · Show 1 · 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 PolyvivalStudios · Jul 31, 2018 at 08:39 AM 0
Share

You should probably try to disable your wave spawner or just disable this script on the "WinGame" function. Example:

First, add a reference to this script:

 public GameController GC;

Second, Disable GC;

public void WinGame() { GameObject[] Enemies = GameObject.FindGameObjectsWithTag("Enemy"); foreach(GameObject Enemy in Enemies) { Destroy(Enemy); } win = true; winText.text = "You Win!!!!!"; gameOver = true; GC.enabled = false; }

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

88 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

Related Questions

End game after 120 seconds HELP 1 Answer

gameover script 2 Answers

How do I give an option to retry a level or return to a level selection menu opposed to just restarting the level? 1 Answer

Game Over Screen appears on launch.,Something with my script makes it so that everytime I run it a gameover happens 1 Answer

When my car crashes into the guards, the game is supposed to be over! 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