How to display the Timer Countdown on the Screen - unity3d

//A simple countdown timer
var myTimer : float = 5.0;
function Update () {
if(myTimer > 0){
myTimer -= Time.deltaTime;
}
if(myTimer <= 0){
Debug.Log("GAME OVER");
}
}
This is a countdowntimer script I just want it to show it up on the screen while it counts down .

Put it OnGUI() such as
function OnGUI () {
GUI.Label (Rect (10,10,150,100), myTimer.ToString());
}

You probably also want to have this outside the Update() method, and have the logic run only when it's necessary. This can be done by using the InvokeRepeating method like this (C#):
float myTimer = 5.0f;
void Start() {
InvokeRepeating( "DecreaseTime", 1, 1 ); // Called every second
}
void DecreaseTime() {
myTimer--;
}
void onGUI() {
GUI.Label(new Rect(10,10,400,90), "myTimer = " + myTimer );
}

Related

How do I spawn repeatedly object whot 0.1 seconds between them?

I want to spawn a 100 object whit 0.1 seconds in between.
I have this but they still spawn all at once.
private float timer = 0.0f;
bool goSpawn = false;
private void Update()
{
if (goSpawn)
{
timer += Time.deltaTime;
}
}
public void spawnAnimals()
{
goSpawn = true;
for (int i = 0; i < 100;)
{
if (timer > 2f)
{
RndSpawnPos = new Vector3(Random.Range(3.30f, 5.70f), 0.78f, Random.Range(-3.00f, 3.01f));
Instantiate(animal, RndSpawnPos, Quaternion.identity);
i++;
timer = 0.0f;
}
}
}
Use an IEnumerator. I put an example to solve the problem:
public void Start() => StartCoroutine(SpawnObject(100, .1f));
public IEnumerator SpawnObject(int count, float intervalTime)
{
for (var i = 0; i < count; i++)
{
Instantiate(" something in here...");
yield return new WaitForSeconds(intervalTime);
}
}

How can I connect button to function?

we have a dash button in the game. If we press the button dash button is working well but the problem is if we press anywhere on screen it is working too! We only want to work that function with button.
Anyone help us please?
Code:
public void Dash()
{
if (Input.GetMouseButtonDown(0))
{
activeMoveSpeed = dashSpeed;
dashCounter = dashLength;
}
if (dashCounter > 0)
{
canShoot = false;
dashCounter -= Time.deltaTime;
if (dashCounter <= 0)
{
canShoot = false;
activeMoveSpeed = speed;
dashCoolCounter = dashCoolDown;
}
}
if (dashCoolCounter > 0)
{
canShoot = false;
dashCoolCounter -= Time.deltaTime;
}
}
private void FixedUpdate()
{
Movement();
Dash();
}
Button and Game
Button
The function you specify in OnClick will be called whenever the button is clicked.
Currently, you are not using the button at all. I think you meant something like this (StartDash() is the function that should be chosen in OnClick):
public void StartDash()
{
activeMoveSpeed = dashSpeed;
dashCounter = dashLength;
}
private void DoDash()
{
if (dashCounter > 0)
{
canShoot = false;
dashCounter -= Time.deltaTime;
if (dashCounter <= 0)
{
canShoot = false;
activeMoveSpeed = speed;
dashCoolCounter = dashCoolDown;
}
}
if (dashCoolCounter > 0)
{
canShoot = false;
dashCoolCounter -= Time.deltaTime;
}
}
private void FixedUpdate()
{
Movement();
DoDash();
}
Take a look at the related pages in the Unity Documentation:
Manual - Button
Scripting - UI.Button
Scripting - UI.Button.onclick

How do I get perform a single button press to call a method?

I would like a Unity progress slider to fill up when I press the spacebar. Currently, I have to mash the spacebar 100 times to fill up the progress bar.
It fills the progress slider automatically when moved to update().
It fills the progress slider when I hold the spacebar if I change Input.GetKeyDown to Input.GetKey.
But I don't want to hold the spacebar down. I want to press it once to start have the slider value gradually fill up the progress bar.
void Start()
{
sliderValue = 1.0f;
}
void Update()
{
pressSpacebar();
}
void pressSpacebar()
{
if (Input.GetKeyDown("space"))
{
filluptheslider();
}
}
void filluptheslider()
{
ProgressSlider.value += sliderValue * Time.deltaTime;
}
There are a few ways to handle this:
Method 1
Boolean value in the class to track the pressed state:
bool pressed = false;
void pressSpacebar()
{
if (Input.GetKeyDown("space"))
{
filluptheslider();
}
if(pressed)
{
filluptheslider();
}
}
Method 2
Coroutine that starts after space is pressed.
void pressSpacebar()
{
if (Input.GetKeyDown("space"))
{
StartCoroutine(FillUpTheSlider);
}
}
IEnumerator FillUpTheSlider()
{
while(ProgressSlider.value < ProgressSlider.maxValue)
{
ProgressSlider.value += sliderValue * Time.deltaTime;
yield return null;
}
}
If you press the space bar multiple times, multiple coroutines will spawn and it will fill faster. You can optionally add the following before you start the coroutine to prevent this behaviour:
StopCoroutine(FillUpTheSlider)
You could alternatively combine method 1 and 2 to remove this behaviour.
bool isFull = false;
bool isPressed = false;
void Update
{
If (Input.GetKeyDown(KeyCode.Space)
&&
!isFull)
{
isPressed = true;
}
if(isPressed)
{
//if you want fill to increase every 0.05 seconds.
StartCoroutine(FillGradually(0.05f));
isPressed = false;
}
}
Ienumerator FillGradually(float fillOffset)
{
//If 100 is your max value
while(ProgressSlider.value < 100)
{
ProgressSlider.value += 1 * Time.deltaTime;
yield return new WaitForSeconds(fillOffset);
}
isFull = true;
}
You can just use a bool condition and when space is pressed, you can just make it true once and when it is true, it will automatically start filling.
bool isSpacePressed; //bydefault, bool class variable is set to false //when initialized
update()
{
if(isSpacePressed==true) //no need to mention ==true just added to make it //readable
{
image.fillAmount=fillvalue+0.1f;
}
}
if(input.GetKey(keyCode.Space)==true)
{
isSpacePressed=true;
}

Trigger a Timer once player enters an object and stops when it enters another

I currently have this code attached to a player to start a timer that ends when I enter a trigger, but I want to make it start when I enter a trigger rather than when I start my game.
public class Timer : MonoBehaviour
{
public Text timerText;
private float startTime;
private bool finished = false;
private bool started = false;
void Update()
{
if (finished)
return;
if (started)
{
float t = Time.time - startTime;
string minutes = ((int)t / 60).ToString();
string seconds = (t % 60).ToString("f2");
timerText.text = minutes + ":" + seconds;
}
}
public void Finish()
{
finished = true;
timerText.color = Color.yellow;
}
void Start()
{
started = true;
}
}
You could do the same thing you've done with the bool finished, but instead it would be something like bool started, which returns true when the player enters the start trigger. Wrap it around the time changing code inside your Update() statement.
Making sure the bools inside Timer are public:
public Text timerText;
public bool finished = false;
public bool started = false;
private float t = 0f;
void Update()
{
if (finished)
return;
if (started)
t += Time.deltaTime;
string minutes = ((int)t / 60).ToString();
string seconds = (t % 60).ToString("f2");
timerText.text = minutes + ":" + seconds;
}
void Started()
{
started = true;
}
And then inside a trigger script, reference your Timer attached to Player.
public class StartTrigger : MonoBehaviour
{
private GameObject player;
private Timer timer;
void Start()
{
player = GameObject.FindWithTag("Ellen");
if (player)
timer = player.GetComponent<Timer>();
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Ellen")
{
timer.Started();
}
}
}

Click and Hold OnMouseDown Unity3D

I want my OnMouseDown command to work only when the player presses over an object within the game. At the moment, the player can press anywhere and the timer will start. Is there a way to have the timer only start when they click and hold an object? To give you some context, I want an object to become inactive when the player clicks and holds over it.
Here is my code so far:
function Update ()
{
if (Input.GetMouseButton(0))
{
timer += Time.deltaTime;
}
if ( Input.GetMouseButtonUp(0))
{
timer = 0;
}
if(timer > 1)
{
gameObject.SetActive(false);
}
}
SOLUTION:
I used a Raycast in order to do this:
var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit;
if (Input.GetMouseButton(0) && coll.Raycast(ray,hit,100))
{
timer += Time.deltaTime;
}
if ( Input.GetMouseButtonUp(0))
{
timer = 0;
}