Я пытаюсь вызвать метод AddFuel из другого сценария, но я не получаю ошибки, но мое топливо не обновляется, пользовательский интерфейс для видов топлива, за исключением того, что он отображает полное топливо (даже если топливо не должно быть полным), но как только я трачу топливо, топливо возвращается к тому, что было за вычетом топлива, которое я потратил.Неправильные методы вызова? C#
Как я могу решить эту проблему?
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() {
}
}
Комментарии не предназначены для расширенного обсуждения; этот разговор был [перемещен в чат] (http://chat.stackoverflow.com/rooms/77194/discussion-on-question-by-schmoopy-calling-methods-incorrectly-c). – Taryn