1е Переименовываешь тему типа "игровая логика, собрать бонус" ну или еще чего, чтоб было всем понятно, и если у когонить будет схожая проблема, и он наберет близкие слова в поиске или гугле, чтоб он нашел эту тему, а не переспрашивал опять.
2е и все остальное
, на unifyComunity берешь скрипт синглетона, (дальше все будет на c# извини, с javaScript не работаю)
Используется csharp
using UnityEngine;
public abstract class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T>
{
private static T m_Instance = null;
public static T instance
{
get
{
// Instance requiered for the first time, we look for it
if( m_Instance == null )
{
m_Instance = GameObject.FindObjectOfType(typeof(T)) as T;
// Object not found, we create a temporary one
if( m_Instance == null )
{
Debug.LogWarning("No instance of " + typeof(T).ToString() + ", a temporary one is created.");
m_Instance = new GameObject("Temp Instance of " + typeof(T).ToString(), typeof(T)).GetComponent<T>();
// Problem during the creation, this should not happen
if( m_Instance == null )
{
Debug.LogError("Problem during the creation of " + typeof(T).ToString());
}
}
m_Instance.Init();
}
return m_Instance;
}
}
// If no other monobehaviour request the instance in an awake function
// executing before this one, no need to search the object.
private void Awake()
{
if( m_Instance == null )
{
m_Instance = this as T;
m_Instance.Init();
}
}
// This function is called when the instance is used the first time
// Put all the initializations you need here, as you would do in Awake
public virtual void Init(){}
// Make sure the instance isn't referenced anymore when the user quit, just in case.
private void OnApplicationQuit()
{
m_Instance = null;
}
}
делаешь свой класс, например GameConfig и екстендишь его с синглетона, туда же пихаешь свою переменную score
Используется csharp
public class GameConfig : MonoSingleton<GameConfig>
{
public int gameScore=0;
}
Далее делаешь пустой объект на него вешаешь скрипт который работает только c GUI и этот скрипт считыват значение score из твоего синглетона
типа
Используется csharp
public class GameHud:MonoBehaviour{
void OnGUI(){
GUI.Label(new Rect(label), "Score:" + GameConfig.instance.score);
}
ну и делаешь контроллер бонуса, который ингрементит общее значение
Используется csharp
public class CoinController:MonoBehaviour{
void OnTriggerEnter(Collider other) {
GameConfig.instance.score+=1;
Destroy(gameObject);
}
}
Если что, извиняюсь за ошибки, потому как все кроме синглетона писал а не копипастил
.