I am trying to call the AddFuel method from a different script but I am getting no error but my fuel wont update, the UI for the fuel kind of updates except it displays full fuel(even if the fuel shouldn't be full) but as soon as I expend fuel the fuel reverts to what it was minus the fuel that I expended.
How can I Solve this Problem?
RefuelPickup Script
void Collect()
{
PlayerController playerController =gameObject.AddComponent<PlayerController>();
float addFuel = .5f;
playerController.AddFuel(addFuel);
}
PlayerController Script
public void AddFuel(float value)
{
fuel += value;
UIController.SetFuel(fuel);
}
UIController Script
using UnityEngine;
using System.Collections;
public class UIController : MonoBehaviour {
private static UIController Instance { get; set; }
public UIRemaining remaining;
public UIFuel fuel;
public UIGameOver gameOver;
public UITimer timer;
/// <summary>
/// Update the UI to show the new number of remaining goals.
/// </summary>
/// <param name="value">The number of goals remaining.</param>
public static void SetRemaining(int value)
{
Instance.remaining.SetValue(value);
}
/// <summary>
/// Set the current value of the fuel bar
/// </summary>
/// <param name="value">A normalised value between 0 and 1, with 0 being empty and 1 being full.</param>
public static void SetFuel(float value)
{
Instance.fuel.SetValue (value);
}
/// <summary>
/// Update the game timer to display a new value
/// </summary>
/// <param name="gameTime">The time, in seconds, to display in the UI</param>
public static void SetTime(float gameTime)
{
Instance.timer.SetTime(gameTime);
}
/// <summary>
/// Display the game over screen with a custom message. This will not show any time information.
/// </summary>
/// <param name="message">The message to display on the game over screen.</param>
public static void GameOver(string message)
{
Instance.gameOver.Show (message);
}
/// <summary>
/// Display the game over screen with a custom message and time information.
/// </summary>
/// <param name="message">The message to display on the game over screen.</param>
/// <param name="time">The last time, in seconds, played in this level</param>
/// <param name="bestTime">The best time, in seconds, played in this level</param>
/// <param name="isNewHighscore">True if the best time is a new highscore, otherwise false.</param>
public static void GameOver(string message, float time, float bestTime, bool isNewHighscore)
{
Instance.gameOver.Show (message, time, bestTime, isNewHighscore);
}
public static void LoadUI()
{
if (Instance == null) {
Application.LoadLevelAdditive("UI");
}
}
void Awake()
{
Instance = this;
}
void OnDestroy()
{
Instance = null;
}
// Use this for initialization
void Start () {
}
}
UIFuel Script
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class UIFuel : MonoBehaviour {
public Slider slider;
public void SetValue (float value)
{
slider.value = value;
}
// Use this for initialization
void Start () {
}
}