x


how can i change the gui text into a sprite instead?

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");
 }
}
more ▼

asked Aug 10 '12 at 09:45 PM

Chris12345 gravatar image

Chris12345
276 2 19 40

I still need all the variables in script 2, i just want a different text on the object.

Aug 10 '12 at 10:01 PM Chris12345
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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");
	}
}
more ▼

answered Aug 10 '12 at 10:22 PM

ScroodgeM gravatar image

ScroodgeM
7.6k 2 6

can you explain in more detail ,sorry i do not understand.

Aug 10 '12 at 10:28 PM Chris12345

details added 8)

Aug 10 '12 at 11:01 PM ScroodgeM

were do the codes go and in what scripts?

Aug 10 '12 at 11:13 PM Chris12345

can i have the 2 scripts put into one script?

Aug 10 '12 at 11:29 PM Chris12345
  1. i'd explain answer.
  2. yes you can have 2 scripts into one.
  3. you should tell us in question that you want somebody to write code for you, not just explain how to realize something
  4. methinks you need some learning in programming.
Aug 10 '12 at 11:42 PM ScroodgeM
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3728
x3678
x379

asked: Aug 10 '12 at 09:45 PM

Seen: 384 times

Last Updated: Aug 10 '12 at 11:56 PM