【发布时间】:2015-05-08 01:38:43
【问题描述】:
我正在尝试从不同的脚本调用 AddFuel 方法,但我没有收到任何错误,但我的燃料不会更新,燃料类型的 UI 更新,除了它显示满燃料(即使燃料不应该是满的) 但是一旦我消耗燃料,燃料就会恢复到减去我消耗的燃料的状态。
我该如何解决这个问题?
加油脚本
void Collect()
{
PlayerController playerController =gameObject.AddComponent<PlayerController>();
float addFuel = .5f;
playerController.AddFuel(addFuel);
}
PlayerController 脚本
public void AddFuel(float value)
{
fuel += value;
UIController.SetFuel(fuel);
}
UIController 脚本
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 脚本
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 () {
}
}
【问题讨论】:
-
评论不用于扩展讨论;这个对话是moved to chat。