Home > Net >  Can I use a variable I got from the player into a text box?
Can I use a variable I got from the player into a text box?

Time:11-29

I'm trying to make an app in Unity which requires your name and other details. I'm using the legacy Input Field to get the answers from the player which worked, but I can't seem to use the Input in any way other than Debug.Log.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ReadInput : MonoBehaviour
{

    private string input;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void ReadStringInput(string s)
    {
        input = s;
        Debug.Log(input);
    }
}

I used this simple script to get the input but I have no clue how to actually use it in the game. It want it to say, for example, Welcome,(Person's Name/Input) outside of the log and actually into a text object.

CodePudding user response:

If you right click in the hierarchy, under UI you can add a text object. I like to use a free package called Text Mesh Pro, which you can get in the asset store, but you don't need to. Once you've made your text object, it should appear as a child of a gameobject called the Canvas. The text object should have a text field that you can access from script.

Text mytext;

public void UpdateMyText(string s)
{
    mytext.text = s;
}

Here's the documentation

Just drag the text object into the script's mytext field in the inspector, and call that function when you want to update your text.

  • Related