- Home /
Changing 3D text through script
So basically, I want a script that shows the number of "players_knights" in a 3D text, but I've searched everywhere and have tried everything but haven't been able to get anything to work. so can someone help me make this script be able to show on to a 3D text? (oh and the most common problem I've had is "Cannot convert "int" to "String" " something like that.)
SCRIPT:
var players_knights = 20;
function Update() {
}
Answer by iwaldrop · Apr 09, 2013 at 01:52 AM
I seriously doubt that you've searched 'everywhere' and tried 'everything'. I tried this, and it worked.
using UnityEngine;
using System.Collections;
public class Text : MonoBehaviour
{
private TextMesh text;
void Awake()
{
text = gameObject.AddComponent<TextMesh>();
text.text = Time.time.ToString();
}
void Update()
{
text.text = Time.time.ToString();
}
}
thanks a bunch :D, but would it be ok if it was java script? I just have never used C#.
Sure, you can do the same stuff in JavaScript. This isn't a site to come and have other people do your coding though, the above was just an example. It's really easy to convert this in to js, so give it a shot! If you have problems, then come back and ask a specific question. :)
Unfortunately, even this is not doing a thing for me :(
See, I have a 3D text in my main scene. I have attached a script to it :
public class updateName : $$anonymous$$onoBehaviour {
private GameController$$anonymous$$aster master_;
private Text$$anonymous$$esh titleText$$anonymous$$esh_;
/*
* Start()
*
* We find the menu's controller.
*/
void Start ()
{
master_ = (GameController$$anonymous$$aster)GameObject.FindGameObjectWithTag (Tags.menuController).GetComponent (typeof(GameController$$anonymous$$aster));
//titleText$$anonymous$$esh_ = (Text$$anonymous$$esh) gameObject.GetComponent (typeof(Text$$anonymous$$esh));
titleText$$anonymous$$esh_ = gameObject.AddComponent<Text$$anonymous$$esh> ();
if(master_.isNameSet())
{
titleText$$anonymous$$esh_.text = "no change";
}
}
/*
* Update()
*
* We check the name again and again.
*/
void Update ()
{
if(master_.isNameSet())
{
titleText$$anonymous$$esh_.text = "no change";
}
else
{
titleText$$anonymous$$esh_.text = "Hello " + master_.getName() + "!";
}
}
}
As you can see, I have tried to GET the 3D text's Text$$anonymous$$esh, without any succees. The text, which I have orginially set in the game view, never changes. BUT I get no error or warning. What may be the problem?
Ah, also : I retrieve the text from a game controller located in another scene used as the menu. Could that be the problem?
Is that maybe because you commented out the GetComponent line and so are no longer linking to the actual object?
Sir, I had no idea what was wrong with my code, and then realized I forgot the () after ToString. It's 3 am, I don't know why I felt the need to make an account and thank you.