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
1
Question by user-2067 (google) · Apr 22, 2010 at 06:36 AM · gameover

gameover script

I have an object in my game that instantiates an enemy every minute. this what i want. I am looking for a script that will count the amount of enemies in the game and let say there can only be 10 of them. So if there are 11 enemies then game-over, load game over screen. I am new at scripting so your assistance will be really helpful.

Thank you so much.

~Sam

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Extrakun · Apr 22, 2010 at 09:08 AM

What you can do is to define an empty game-object called 'controller', and attach a script like this to it

gameOver : boolean = false;

function Update() { if (enemyCount > 11) gameOver = true;

}

function OnGUI() { if (gameOver) GUI.Label(Rect(0,0, 100, 20), "Game Over!);

}

function enemySpawned() { enemyCount++; }

Of course, this is just the concept, not a solution. What you need to do is each time an enemy is spawned, inform the controller to increase the count of enemy by 1. You can use send message

Find("controller").SendMessage("enemySpawned");

There are many ways to do this. Experiment! :D

Next, the gist is that every game update, the script will check if the game over condition is met. If so, a boolean is set. The OnGUI() will detect that the game is over, and renders the game over message. You will also want to pause the gameplay, plays game ending music and etc. You can either have other scripts check for the gameOver boolean (which will do for a beginner's project), or freeze the game just to show a game over dialog box, or even jump to new a scene.

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
1

Answer by Novodantis 1 · Apr 22, 2010 at 09:35 AM

Here's a slightly alternative solution. Make a new JS script on an empty GameObject and replace its contents with the code below:

var baddieLimit = 10;

private var baddies : BadGuy[];

function Update() { CheckGameOver(); }

function CheckGameOver() { baddies = FindObjectsOfType (BadGuy); // get a list of baddie objects

 if (baddies.length > baddieLimit) {
      Application.LoadLevel(GameOver);
      // if there are more than baddie limit, go to Game Over scene
 }

}

Note: "BadGuy" should be replaced with the name of your enemy script, and GameOver the name of the unity scene file that contains a Game Over screen. It's also good practice to put your enemy limit into a variable too, so you can tweak it at leisure

What this does is, each frame it counts all the game objects that have a bad guy script, and if it exceeds the baddie limit value then go to a game over scene (you can just as easily replace this bit with GUI text, as in the example above). It should be stated that this is not as efficient as the above script, in that it searches for objects every frame instead of checking a value that is incremented with each spawn, but it may be easier to set up at first.

Comment
Add comment · Show 2 · 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 user-2067 (google) · Apr 25, 2010 at 08:50 AM 0
Share

thank you guys...i have it working now:)

avatar image user-2067 (google) · Apr 30, 2010 at 08:50 AM 0
Share

thank you guys..this is helpful

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

No one has followed this question yet.

Related Questions

Menu communication failed 1 Answer

How to destroy and respawn the player after loosing a life? 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

How to make a dramatic big finish? 1 Answer

How do I make Game Over text stay enabled for a few seconds before the Application Load function runs? 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