Search Unity

How do you change Text UI without using a button UI component

Discussion in '2D' started by misterjoe, Feb 4, 2020.

  1. misterjoe

    misterjoe

    Joined:
    Dec 20, 2019
    Posts:
    3
    This may be a newbie question. I am trying to use an API call, and take one of the elements from that and put it into a Text UI component. I've first been trying to attach the component to the Text UI with no success. Here are some images to better explain my issue...

    Then Here's the script

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class MathApiCall : MonoBehaviour
    7. {
    8.     public Text Problem;
    9.  
    10.     public void start()
    11.     {
    12.         Problem.text = "Hello Friend";
    13.     }
    14. }
    15.  
     

    Attached Files:

  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,464
    You need to assign the Text component on the gameobject (in the inspector) onto the script. Click and drag it to the field next to "Problem" on your script in the inspector on the gameobject.
     
    misterjoe likes this.
  3. misterjoe

    misterjoe

    Joined:
    Dec 20, 2019
    Posts:
    3
    @Cornysam Sorry, let me describe what I meant. I want to change the text, so I added a script to the text ui component as displayed about. The UI is displayed above properly. It's just that the text won't change.
     
  4. vinceghi

    vinceghi

    Joined:
    Jan 29, 2020
    Posts:
    6
    And he still gave you the correct answer. You never tell Unity what Text Component of wich Object the "Problem" variable is. As you can see in your screenshot... there is your public variable Problem in the Inspector and there is a big "None" next to it.. wich means.. that there is no Text component assigned to the variable. Go into the inspector... and move the Text Object you want to change into the field next to "Problem" or use the code below. Only works if you attach the script to and TextUI Object.

    Code (CSharp):
    1. Problem = gameObject.GetComponent<Text>();
    2. Problem.text = "Hello Friend";
    3.  
    4.  
     
  5. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,464
    If you want to be able to change it, create a string variable and set Problem.text = stringVariable and then you can set it in the inspector.
     
    misterjoe likes this.
  6. misterjoe

    misterjoe

    Joined:
    Dec 20, 2019
    Posts:
    3
    Oo I didn't realize that's what you were saying. Please excuse my noobness.

    In addition, I changed the Start method to Awake instead it changed the text! Thanks for the feedback everyone!