Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 DoodleMumble · May 20, 2014 at 06:58 AM · timerstoptimer-script

How do I stop my timer when all of the "dummies" are destroyed?

Pardon my badly written script. I am new to scripting.

I am trying to make a simple game where you run around and shoot the "dummies". I want to put into my code that when all of the dummies have been shot, then my timer will stop, however all i can get it to do is stop when i only destroy one of them. The way I have tried to do it is when one of the dummies have been destroyed, then count up 1. all the way up to 16(the amount of dummies). I am not stuck on this counting up method if you have another way of doing this. If someone could post on here and help me out, I would greatly appreciate it.

Here are my script for it... (again please excuse the sloppyness :D)

 using UnityEngine;
 using System.Collections;
 
 public class Timer2 : MonoBehaviour {
 
     public float startTime;
     private float elapsedTime;
     public float dummiesDead = 15;
 
     public GameObject dummy1;
     public GameObject dummy2;
     public GameObject dummy3;
     public GameObject dummy4;
     public GameObject dummy5;
     public GameObject dummy6;
     public GameObject dummy7;
     public GameObject dummy8;
     public GameObject dummy9;
     public GameObject dummy10;
     public GameObject dummy11;
     public GameObject dummy12;
     public GameObject dummy13;
     public GameObject dummy14;
     public GameObject dummy15;
     public GameObject dummy16;
 
 
     //TimerGUI
     public GUIStyle myStyle;
 
     public Rect rect = new Rect(300, 25, 100, 200);
 
     void Awake(){
         startTime = 0;      
     }
     
     void Update () {
         
         if (startTime > 0)
         {
             elapsedTime = Time.time - startTime;
         }
         if( dummy1 == null){
 
             dummiesDead = +1;
         }
 
         if( dummy2 == null){
             
             dummiesDead = +1;
         }
 
         if( dummy3 == null){
             
             dummiesDead = +1;
         }
 
         if( dummy4 == null){
             
             dummiesDead = +1;
         }
 
         if( dummy5 == null){
             
             dummiesDead = +1;
         }
 
         if( dummy6 == null){
             
             dummiesDead = +1;
         }
 
         if( dummy7 == null){
             
             dummiesDead = +1;
         }
 
         if( dummy8 == null){
             
             dummiesDead = +1;
         }
 
         if( dummy9 == null){
             
             dummiesDead = +1;
         }
 
         if( dummy10 == null){
             
             dummiesDead = +1;
         }
 
         if( dummy11 == null){
             
             dummiesDead = +1;
         }
 
         if( dummy12 == null){
             
             dummiesDead = +1;
         }
 
         if( dummy13 == null){
             
             dummiesDead = +1;
         }
 
         if( dummy14 == null){
             
             dummiesDead = +1;
         }
 
         if( dummy15 == null){
             
             dummiesDead = +1;
         }
 
         if( dummy16 == null){
             
             dummiesDead = +1;
         }
 
         if (dummiesDead == 16f) {
         
             startTime = 0;
         
         }
 
     }
     
     void OnTriggerEnter(){
         startTime = Time.time;
     }
 
     
     void OnGUI(){
         GUI.Label( rect , (elapsedTime.ToString("F2")),myStyle);
         rect.x = 20f;
         rect.y = 20f;
     }
 
 }
 
Comment
Add comment · Show 2
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 $$anonymous$$ · May 20, 2014 at 07:25 AM 1
Share

Wow that is one of the most inneficient scripts ive ever seen. Your "sloppiness" is nuts :D

avatar image Fornoreason1000 · May 20, 2014 at 09:50 AM 0
Share

@$$anonymous$$yIsaak, if you're going to make criticism why don't you inform the OP why his script is inefficient, sure me and you can see why, but he can't.

2 Replies

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

Answer by cryingwolf85 · May 20, 2014 at 09:40 AM

I'm not big on the idea of "writing code for people", but I hate to see someone who is putting forth the effort and writing their own code but going in the wrong direction.

The structure of this is very wrong. I understand you are new to programming, and that's why i'm going to explain things with some detail. I've re-created things for you with explanation comments.

Rather than checking constantly if the game time is over in the update loop, only check it when a dummy is destroyed. Now that that's clear, lets take a look at a master game controller i've whipped up for you:

 using UnityEngine;
 using System.Collections;
 
 public class GameController : MonoBehaviour {
 
     private static int dummyAmount;
     private static float startTime, endTime;
 
     public void Start(){
 
         // Get how many dummies are in the scene
         dummyAmount = GameObject.FindGameObjectsWithTag("Dummy").Length;
     }
 
     public static void RemoveDummy(){
 
         // Remove a dummy
         dummyAmount--;
 
         // <= than zero to prevent any weird bugs (from experience)
         if(dummyAmount <= 0){
 
             /* Calculate the elasped time */
             endTime = Time.time;
             float elapsedTime = endTime - startTime;
 
         }
     }
     
     void OnTriggerEnter(){
 
         // Not sure what trigger you are entering, this could be something you
         // would be starting from another script, and setting the time from
         // there
         startTime = Time.time;
     }
 
 }

Right now, this will work if you attach it to whatever object with the trigger that the player is entering. You can retrieve the final time elapsed however you would like, since it is calculated.

Now for the bots. When you create them, put them all under the tag "Dummy", and add this script to each:

 using UnityEngine;
 using System.Collections;
 
 public class Dummy : MonoBehaviour {
 
     public float health = 100;
     
     public void TakeHit(float damage){
 
         // Not sure how you are removing dummies, but just for fun ;)
         health -= damage;
 
         // Replace with whatever condition must be met for the dummy to die
         if(health <= 0){ 
             
             // Destroy this game object, will run OnDestroy
             Destroy(gameObject);
         }
 
     }
 
     public void OnDestroy(){
 
         // Call the method to subtract a dummy and check if they are gone
         GameController.RemoveDummy();
     }
     
 }

This will ensure that when the dummy is destroyed, it informs the GameController and it accounts for the dummies removal. No need to keep track of dummy objects up in this joint.

I hope you learn something from this code. The best way to learn to code is by example code.

Cheers

Comment
Add comment · Show 5 · 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 Fornoreason1000 · May 20, 2014 at 10:06 AM 0
Share

Good Answer!

avatar image cryingwolf85 · May 20, 2014 at 05:57 PM 0
Share

Thank you! I come from Stack Overflow - quite different than here it seems, lol.

avatar image DoodleMumble · May 21, 2014 at 05:41 AM 0
Share

Thank you soo much!!!

I really appreciate it man :D

avatar image cryingwolf85 · May 21, 2014 at 05:24 PM 0
Share

No problem! Select as answer?

avatar image Fornoreason1000 · May 25, 2014 at 04:48 AM 0
Share

please accept cryingwolfs answer if it works

Theirs a tick icon below the up vote and down votes, please click it to show appreciation of his time.

avatar image
0

Answer by Fornoreason1000 · May 20, 2014 at 09:38 AM

you want arrays, you need arrays. arrays are not bad... they are good if you know how to use them. so basically instantiate all your dummies and place them in a array. that way you can keep track of all of them much more easily. in this example a i made a static array of your dummies to keep track of them, even made a nifty function that creates any number of them.

 public static GameObject[] dummies;
 
 //num is the number of dummies you want
 public void CreateDummies( int num ) {
 for(int i =0; i < num; i++) {
 Instantiate(myDummyObject)p
 
 }
 }

now to check if they are all dead? we we could enumerate through the entire array and waste a huge amount of time or we can just check if its empty, I made it static to allow it to be accesed fropm your dummies.

     public static bool DummieHordeIsDead() {
     return dummies == GameObject[]();
 
 }

just some pseudo code to help explain it... another way is to put a script on your dummies that raise death flags to this script. as crying wolf suggested

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

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

22 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

Related Questions

How to I get the timer counter to restart when the player hits the ground and respawns? 2 Answers

How do I implement a timer into this script that will record the total time the player has played. 2 Answers

Timer Text doesn't show the time in the script. 1 Answer

Coroutines pauses when not focused on the game page. 1 Answer

Not sure what is wrong with my timer script. 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