Hello.
I wish to develop a simply 2D game in unity. I would like to create empty gameObject that will pass 3 parameters to their constructors in c#. The first parameter will be a background color, and the others are 2 letters I wish to be added to the gameObject.
Can you please help me?
As a start I would suggest you take a look at the TextMesh component, that should enable you display the two letters.
For the background you could just use a plain sprite and set its colour through the SpriteRenderer.Color property.
I am very new to Unity3D. I have a good background at WPF, and I know I to do that with label in that platform. Can you please show a code example? I tried to add a SpriteRenderer and got an error message.
First of all setup a GameObject in your scene like this…
-
- Save a blank white png file to your project to use as the background sprite, set it’s anchor to Top Left.
-
- Create an empty GameObject and call it something like TextObject.
-
- Add a TextMesh Component to that gameobject and set its Offset Z to -1 and change its font property (by default the only one you can select is arial).
-
- Drag the white background sprite onto the TextObject game object to make it a child of the TextObject.
Next create a script and name it TextObject. Copy the code below into the script and save it.
using UnityEngine;
using System.Collections;
public class TextObject : MonoBehaviour {
public SpriteRenderer backGround;
public TextMesh textMesh;
public Color backGroundColour = Color.white;
public Color textColour = Color.black;
public string text = "AB";
// Use this for initialization
void Start () {
backGround.color = backGroundColour;
textMesh.color = textColour;
textMesh.text = text;
}
// Update is called once per frame
void Update () {
}
}
Now add this script to your TextObject gameobject in your scene.
Then in the inspector do the following…
- Drag the TextMesh component of your TextObject into the textMesh field of the script.
- Drag the background sprite (that you added as a child to your gameobject) into the background field of the script.
- Set the backGroundColour, textColour and text fields to suitable values and you should be good to go.
You can also change the backGroundColour, textColour and text properties from another script at runtime if you need to.
Note: To make the text look sharper, increase the value of the TextMesh FontSize property and reduce the value of the TextMesh CharacterSize property.
Hope this helps.
First of all, thanks for your help. I followed your instructions step by step and got this error message: “The associated script cannot be loaded. Please fix any compile errors and assign a valid script”. What I’m doing wrong here?
Sounds like you have an error in the script you’re attaching to the game object.
If you can’t track the error down yourself post the script here and myself or someone else should be able to give you some advice.
I found out what was my mistake. The file name and the object I was creating in c# had different names. How do I make a text I write appear in 2 lines?
use \n in the string anywhere you want a line break.
Again - thanks a lot. How do you create a rich text? I want the first line to have one font and the second line to have a different font and font size.
Unity Rich Text isn’t what you probably understand as Rich Text, and doesn’t support multiple fonts in the same string, as far as I know, although you can change the size and some other properties.
See this for more details. https://docs.unity3d.com/Documentation/Manual/StyledText.html