Michael,
That’s quite a request, but I like a challenge, so here we go.
Firstly, the structure that probably best suits this is:
- Game Controller (in charge of game win/loss and messaging)
- Timer (GuiText object that displays the time left on screen. It is VERY important that Timer is a child of the Game Controller gameobject)
- Items (the onscreen, destructable items you mentioned. These must also be child objects of the Game Controller)
The Game Controller would look something like this:
using UnityEngine;
using System.Collections;
public float timeIncrease = 2;
public bool timeElapsed = false;
public int items;
void Start()
{
//Gather how many items are remaining
GameObject[] items = GameObject.FindObjectsWithTag("items") as GameObject[];
itemsRemaining = items.length;
//The timer, as a child of this gameobject, receive this and start the countdown using the timeRemaining variable
BroadcastMessage("Start Timer", timeRemaining);
}
void Update()
{
if (itemsRemaining == 0)
{
//You win!
}
if (timeElapsed)
{
//You lose!
}
}
//If the game controller receives this signal from the timer, it will end the game
void timeHasElapsed()
{
timeElapsed = true;
}
//If the Game Controller receives this signal from a destroyed item,
// it sends a message to the time object to increase the time left
void itemDestroyed()
{
increaseTime();
}
void increaseTime()
{
broadcastMessage("timeIncrease", timeIncrease)
}
//I've included this dead function because I can't test the code myself right now and I don't want to leave
// you with errors. IT may or may not be needed, though.
void timeIncrease
{}
The purpose of the Game Controller would be to receive the happenings of both the items and timers and pass the appropriate responses back and forth, as well as handle the win/lose condition.
Then, you would have this script attached to your timer object:
using UnityEngine;
using System.Collections;
public float timeRemaining = 60f;
void Start()
{
InvokeRepeating("decreaseTimeRemaining", 1.0, 1.0)
}
void Update()
{
if (timeRemaining == 0)
{
sendMessageUpward("timeElapsed");
}
GuiText.text = timeRemaining + " Seconds remaining!";
}
void decreaseTimeRemaining()
{
timeRemaining --;
}
//may not be needed, left it in there
void timeElapsed()
{}
And, in the script for your onscreen items, you would include this:
void OnDestroy()
{
SendMessageUpwards("itemDestroyed")
}
void itemDestroyed()
{}
I have to apologise in advance, though, I have no ability to test this code right now and I had to crank it out quickly, so it will have errors, but remember, Game Controller is the parent to the timer and items, as they will have to send messages back and forth and, in this code, the game controller will handle that.
That's definitely Javascript. The asker requested C#.
– DESTRUKTORRTranslated to C# it would be float timeLeft = 30.0f; void Update() { timeLeft -= Time.deltaTime; if(timeLeft < 0) { GameOver(); } } -Thanks to Stone Legion for telling me that timeLeft is meant to be a float.
– HarryGoddenint timeLeft = 30; should be a float, not int as Time.deltaTime returns a float.
– Stone-LegionHello, I know it should work but apparently i do something wrong. Wrote your code in Update function but it is not working. When i set "float timeLeft = 30.0f;" it is always like 29,98108 and changing only 3 last digits.
– QQQ_QQQpublic float blablabla = 10 ; void Update() { blablabla -= Time.deltaTime; if( blablabla < 0) {print ("Rutter cool"); } }
– CodyBlack