- Home /
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;
}
}
Wow that is one of the most inneficient scripts ive ever seen. Your "sloppiness" is nuts :D
@$$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.
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
Thank you! I come from Stack Overflow - quite different than here it seems, lol.
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.
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
Your answer
Follow this Question
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