How to Add time to my timer on colliding
Hello I would like to know why the scripts I'm using are not working properly with my timer i would really appreciate any help !
using System;
using System.Collections.Generic;
using UnityEngine;
public class Timertext : MonoBehaviour
{
public float timeLeft;
public GUIText Timer;
public GUIText gameOverText;
public void Update()
{
timeLeft -= Time.deltaTime;
if (timeLeft <= 0.0f) {
Timer.text = "You ran out of time";
gameOverText.text = "GAME OVER";
}
else
{
Timer.text = "Time left = " + (int)timeLeft + " seconds";
}
}
}
using UnityEngine;
using System.Collections;
public class Tanktimer : MonoBehaviour {
public Timertext Timertext;
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "PickUp")
{
//add time
//Destroy(other.gameObject);
Timertext.timeLeft += 10;
}
}
}
did you initialized ou timer with any value? (it's not on the script, so if you didn't gave it any value don't forget to do so), i know it's a silly question but everyone forget details sometimes, so yeah.
$$anonymous$$oved Q to HelpRoom area. Debugging Q's like this are one of the things it's for.
Good job. Thought I'd done that already :)
Answer by meat5000 · Dec 02, 2015 at 02:35 PM
public Timertext Timertext; //BAD - Dont name a variable to be the same as its Type
Try
public Timertext timerText;
Then change
timerText.timeLeft += 10;
If you are trying to access a particular script rather than the instance in your script, you will need to reference the gameobject containing the script and use GetComponent to modify the information.
I already tried it but there is still an error
using UnityEngine;
using System.Collections;
public class Tanktimer : $$anonymous$$onoBehaviour {
public Timertext timerText;
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "PickUp")
{
//add time
//Destroy(other.gameObject);
timerText.timeLeft += 10;
}
}
}
Your answer
Follow this Question
Related Questions
Player bounces off of enemy. Help!! 1 Answer
punrpc fails 0 Answers
bool not working on another cloned enemy 1 Answer