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

Question Issue with strings and texts.

Discussion in 'Scripting' started by Quest9, Dec 16, 2022.

  1. Quest9

    Quest9

    Joined:
    Dec 16, 2022
    Posts:
    3
    I'm pretty new to coding and I can't figure this out. I've been following this tutorial and I've checked multiple times that I've copied the text correctly, and I didn't see any errors. What it's supposed to do is get info from an inventory slot and then display that on text. The issue I've been having is that I can't convert the string that makes the information to text. It doesn't update to the new text. I've confirmed that the string is correctly getting the information I want shown. The code I've copied does have an error saying that "tooltipText" (on line 28) doesn't exist in the current context and I've changed it to get rid of that error but it still doesn't work.

    My Code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Tooltip : MonoBehaviour
    7. {
    8.     private Text tooltip;
    9.  
    10.     void Start ()
    11.     {
    12.         tooltip = GetComponentInChildren<Text>();
    13.         tooltip.gameObject.SetActive(false);
    14.     }
    15.     public void GenerateTooltip(Item item)
    16.     {
    17.         string statText = "";
    18.         if (item.stats.Count > 0)
    19.         {
    20.             foreach(var stat in item.stats)
    21.             {
    22.                 statText += stat.Key.ToString() + ": " + stat.Value.ToString() + "\n";
    23.             }
    24.         }
    25.        
    26.         string tooltip = string.Format("<b>{0}</b>\n{1}\n\n<b>{2}</b>", item.title, item.description, statText);
    27.  
    28.         tooltipText.text = tooltip;
    29.         gameObject.SetActive(true);
    30.     }
    31. }
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,016
    So you define a field called
    tooltip
    , however on line 26 you kinda 'hide' the field by declaring a local variable with same name.

    Then you try to call
    tooltipText
    , which doesn't exist.

    I think you can figure out where to go from here?

    EDIT: Looks like you are following the tutorial correctly, it's just... wrong? I'd find a better tutorial.
     
  3. Quest9

    Quest9

    Joined:
    Dec 16, 2022
    Posts:
    3
    That's what I was fearing. But he's able to continue just fine, which I don't understand. Is there anyway to get a string to display on text? That string (on line 26) does output it correctly when I print it on the console but I don't know how to get it to work with the text.
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,016
    Well I would revise the last part of the script as so:
    Code (CSharp):
    1. //rename this so we don't hide the tooltip field
    2. string tooltipString = string.Format("<b>{0}</b>\n{1}\n\n<b>{2}</b>", item.title, item.description, statText);
    3.  
    4. tooltip.text = tooltipString; //correctly assign the string to your text component
    5. tooltip.gameObject.SetActive(true); //set the component active (because it was made inactive before)
    Part of the issue was the text component being made inactive and probably never set active.

    It doesn't look like whoever wrote this tutorial checked it after the fact.
     
    Last edited: Dec 16, 2022
    Quest9 likes this.
  5. Quest9

    Quest9

    Joined:
    Dec 16, 2022
    Posts:
    3
    Thank you for the speedy response. It works now!
     
    spiney199 likes this.