[4.6 UI Text] How to display variables along side typed text.

Basically, what i want to do is get this …

("Health: “_active.hp” Defence: “_active.defence” Speed: "_active.speed);

to turn into this…

Health : 50 Defence: 50 Speed: 50

but i get errors like… "unexpected symbol “_active” "

i am sure this is a 10 second fix but i just cant figure it out :frowning:

2088549–136582–S_DisplayStats.cs (533 Bytes)

You can try:

string myOutput = "Health: "+_active.hp+" Defence: "+_active.defence+" Speed: "+_active.speed;

Text textComponent = GetComponent<Text>();

textComponent.text = myOutput;
2 Likes

You can always use .ToString() on anything to convert it to a human readable string.

ya you can just add the values on, using .ToString() to convert them to a string. Or a other option is using c# string formatting

int pricePerOunce = 5;
String s = String.Format("The current price is {0} per ounce.", pricePerOunce);
// Result: The current price is 5 per ounce.

lets you use place holders, in a text string, and fill them in using the args given after the string.
the 2nd arg giveing will fill in the {0} place holder

I am still getting errors i tried your code Fajlworks, but i get errors saying “NullRefferenceException: Object reference not set to an Instant of an object”

This is what i have…

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

    public class S_DisplayStats : S_Pokedex{

    // Use this for initialization
    void Start () {
        //display player stats
        string playerStats =  (
            "Pokemon: " +_active.pokemonName+
                "   Health: " +_active.hp+
                "   Defence: " +_active.defence+
                "   Speed: " +_active.speed+
                "   Strength: " +_active.strength+
                "   Type: " +_active.type);
       
        Text textComponent = GetComponent<Text> ();
        textComponent.text = playerStats;
    }
   
    // Update is called once per frame
    void Update () {
   


    }
}

Is it a problem with inheriting?

You can try:

Debug.Log("textComponent:"+ textComponent);
Debug.Log("active mon:"+ _active);

That way you can check which variable is null at the time.

If textComponent is null after using GetComponent(), I’d say you forgot to add component to your game object. If active is null, well, I guess your bug is in another castle. >>

1 Like