Hi there. I have only been exploring Unity for a few weeks, so this question might be a simple one. I am making a platformer game, where a monster character eats fruit to gain points before the time runs out. I have made some progress bars but don’t know how to rig them up to talk to each other. I have used a few different tutorials, so I’m a little muddled of where to put things properly. Right now, when I press play, the timer counts down but the bar goes directly to zero. Ideally, I would like the bar to read Time Remaining: (min:sec). For the points bar, I think I just don’t know where to hook up the score value. I have a PlayerController, but the code for it doesn’t update the points bar. I would be eternally grateful for any help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BarScript : MonoBehaviour {
private float fillAmount;
[SerializeField]
private float lerpSpeed;
[SerializeField]
private Image content;
[SerializeField]
private Text valueText;
[SerializeField]
private Color fullColor;
[SerializeField]
private Color lowColor;
[SerializeField]
private bool lerpColors;
public float MaxValue { get; set; }
public float Value{
set{
string[] tmp = valueText.text.Split (':');
valueText.text = tmp [0] + ": " + value;
fillAmount = Map (value, 0, MaxValue, 0, 1);
}
}
// Use this for initialization
void Start () {
if(lerpColors){
content.color = fullColor;
}
}
// Update is called once per frame
void Update () {
HandleBar ();
}
private void HandleBar(){
if (fillAmount != content.fillAmount) {
content.fillAmount = Mathf.Lerp (content.fillAmount, fillAmount, Time.deltaTime * lerpSpeed);
}
if (lerpColors) {
content.color = Color.Lerp (lowColor, fullColor, fillAmount);
}
}
private float Map(float value, float inMin, float inMax, float outMin, float outMax){
return (value - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
[Serializable]
public class Stat {
[SerializeField]
private BarScript bar;
[SerializeField]
private float maxVal;
[SerializeField]
private float currentVal;
public float CurrentVal{
get{
return currentVal;
}
set{
this.currentVal = Mathf.Clamp(value,0, MaxVal);
bar.Value = currentVal;
}
}
public float MaxVal {
get{
return maxVal;
}
set{
this.maxVal = value;
bar.MaxValue = maxVal;
}
}
public void Initialize(){
this.MaxVal = maxVal;
this.CurrentVal = currentVal;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CountDownTimer : MonoBehaviour {
public string levelToLoad;
private float timer = 180f;
private Text timerSeconds;
// Use this for initialization
void Start () {
timerSeconds = GetComponent<Text> ();
}
// Update is called once per frame
void Update () {
timer -= Time.deltaTime;
timerSeconds.text = timer.ToString ("f2");
if (timer <= 0) {
Application.LoadLevel (levelToLoad);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerController : MonoBehaviour {
public Vector2 moving = new Vector2 ();
[SerializeField]
private Text scoreText;
private int score;
// Use this for initialization
void Start () {
score = 0;
UpdateScore ();
}
// Update is called once per frame
void Update () {
moving.x = moving.y = 0;
if (Input.GetKey ("right")) {
moving.x = 1;
} else if (Input.GetKey ("left")) {
moving.x = -1;
}
if (Input.GetKey ("up")) {
moving.y = 1;
} else if (Input.GetKey ("down")) {
moving.y = -1;
}
}
public void AddScore(int newScoreValue){
score += newScoreValue;
UpdateScore ();
}
void UpdateScore (){
scoreText.text = "Fruit Points: " + score;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Collectable : MonoBehaviour {
public int scoreValue;
private PlayerController playerController;
// Use this for initialization
void Start () {
GameObject playerControllerObject = GameObject.FindWithTag ("PlayerController");
if (playerControllerObject != null) {
playerController = playerControllerObject.GetComponent<PlayerController>();
}
if (playerController == null) {
Debug.Log ("Cannot find 'PlayerController' script");
}
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D(Collider2D target) {
if (target.gameObject.tag == "Player") {
playerController.AddScore (scoreValue);
Destroy (gameObject);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerBar : MonoBehaviour {
[SerializeField]
private Stat fruitPoints;
[SerializeField]
private Stat timeLeft;
private float CountDownTimer;
private float currentTime = 180f;
private int ScoreValue;
private int currentPoints = 0;
private void Awake(){
fruitPoints.Initialize ();
timeLeft.Initialize ();
}
// Update is called once per frame
void Update () {
if (ScoreValue > currentPoints) {
fruitPoints.CurrentVal += 5;
currentPoints += 5;
}
if (CountDownTimer < currentTime) {
timeLeft.CurrentVal -= 1;
currentTime -= 1;
}
}
}
I’m mostly trying to update the values of the bar in PlayerBar script. Perhaps there is a way of combining these scripts together or I have to put them in a different place? I’m so new that I don’t know exactly how to do that without breaking it yet.


