first script
need this to be switched out with script bellow, so that it is not gui anymore but is this different text?
using UnityEngine;
using System.Collections;
public class TextMeshExample : MonoBehaviour {
tk2dTextMesh textMesh;
int score = 0;
// Use this for initialization
void Start () {
textMesh = GetComponent();
}
// Update is called once per frame
void Coin(){
score += 1;
textMesh.text = "" + score.ToString();
textMesh.Commit();
}
}
..................................................next script.....................
this is the one i want to to change.
using UnityEngine;
public class GUIManager : MonoBehaviour {
public GUIText distanceText;
private static GUIManager instance;
void Start () {
instance = this;
}
void Update () {
GameEventManager.TriggerGameStart();
}
public static void SetDistance(float distance){
instance.distanceText.text = distance.ToString("f0");
}
}
so what’s the trouble? make a first script also singletone, make there static method to receive new text, and call it whenever you need.
if it can’t be a singletone, declare public variable of type ‘TextMeshExample’ in 2nd script
public TextMeshExample textMeshExample;
assign GameObject with TextMeshExample script attached to GameObject that keeps 2nd script, for it you need save script, go to inspector and drag-n-drop GO with 1st script to GO with 2nd script, exactly to variable ‘textMeshExample’ we just made.
now, add a method to TextMeshExample that will let us set custom text there:
public void SetCustomText(string text)
{
textMesh.text = text;
textMesh.Commit();
}
and the last, call from second script the first one. (in SetDistance method?)
textMeshExample.SetCustomText("my text");
first script - result
using UnityEngine;
using System.Collections;
public class TextMeshExample : MonoBehaviour
{
tk2dTextMesh textMesh;
int score = 0;
// Use this for initialization
void Start()
{
textMesh = GetComponent();
}
void Coin()
{
score += 1;
textMesh.text = " " +score.ToString();
textMesh.Commit();
}
public void SetCustomText(string text)
{
textMesh.text = text;
textMesh.Commit();
}
}
second script - result
using UnityEngine;
public class GUIManager : MonoBehaviour
{
public GUIText distanceText;
public TextMeshExample textMeshExample;
private static GUIManager instance;
void Start()
{
instance = this;
textMeshExample.SetCustomText("my text");
}
void Update()
{
GameEventManager.TriggerGameStart();
}
public static void SetDistance(float distance)
{
instance.distanceText.text = distance.ToString("f0");
}
}