Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

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

Discussion in 'Scripting' started by SpartanWrath, Apr 28, 2015.

  1. SpartanWrath

    SpartanWrath

    Joined:
    Apr 28, 2015
    Posts:
    2
    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 :(
     

    Attached Files:

  2. Fajlworks

    Fajlworks

    Joined:
    Sep 8, 2014
    Posts:
    344
    You can try:

    Code (CSharp):
    1. string myOutput = "Health: "+_active.hp+" Defence: "+_active.defence+" Speed: "+_active.speed;
    2.  
    3. Text textComponent = GetComponent<Text>();
    4.  
    5. textComponent.text = myOutput;
     
    SpartanWrath and Kiwasi like this.
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You can always use .ToString() on anything to convert it to a human readable string.
     
  4. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    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
    Code (CSharp):
    1. int pricePerOunce = 5;
    2. String s = String.Format("The current price is {0} per ounce.", pricePerOunce);
    3. // 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
     
  5. SpartanWrath

    SpartanWrath

    Joined:
    Apr 28, 2015
    Posts:
    2
    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...

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5.     public class S_DisplayStats : S_Pokedex{
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.         //display player stats
    10.         string playerStats =  (
    11.             "Pokemon: " +_active.pokemonName+
    12.                 "   Health: " +_active.hp+
    13.                 "   Defence: " +_active.defence+
    14.                 "   Speed: " +_active.speed+
    15.                 "   Strength: " +_active.strength+
    16.                 "   Type: " +_active.type);
    17.        
    18.         Text textComponent = GetComponent<Text> ();
    19.         textComponent.text = playerStats;
    20.     }
    21.    
    22.     // Update is called once per frame
    23.     void Update () {
    24.    
    25.  
    26.  
    27.     }
    28. }
    29.  
    Is it a problem with inheriting?​
     
  6. Fajlworks

    Fajlworks

    Joined:
    Sep 8, 2014
    Posts:
    344
    You can try:
    Code (CSharp):
    1. Debug.Log("textComponent:"+ textComponent);
    2. Debug.Log("active mon:"+ _active);
    That way you can check which variable is null at the time.

    If textComponent is null after using GetComponent<Text>(), 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. >_>
     
    Kiwasi likes this.