How do I exchange text in unity3d with a script I can’t remove?

Asked

Viewed 2,113 times

4

I have a problem in unity3d and I don’t know how to solve.

I have a certain script

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

public class CharacterSelector : MonoBehaviour

{
    [SerializeField]

    private Text bio1 = null;
    private Text data2 = null;
    private Text name = null;
    private Text subname = null;
    private GameObject[] characterList;
    private int index;

    private void Start()
    {
        characterList = new GameObject[transform.childCount];

        for (int i = 0; i < transform.childCount; i++)
        {
            characterList[i] = transform.GetChild(i).gameObject;
            foreach (GameObject f in characterList)
            {
                f.SetActive(false);
                if (characterList[0])
                    characterList[0].SetActive(true);
            }
        }
    }

    public void ToggleLeft()
    {
        //desligar o modelo atual
        characterList[index].SetActive(false);
        index--;
        if (index < 0)
            index = characterList.Length - 1;
        //ligar o modelo atual
        characterList[index].SetActive(true);
    }

    public void ToggleRight()
    {
        //desligar o modelo atual
        characterList[index].SetActive(false);
        index++;
        if (index == characterList.Length)
            index = 0;
        //ligar o modelo atual
        characterList[index].SetActive(true);
    }

    public void COnfirmButton()
    {
        if (index == 0) 
        {
            SceneManager.LoadScene("NeroRoom");             
        }
        if (index == 1) 
        {
            SceneManager.LoadScene("Menu");
        }

    }
    public void ChangeText() 
    {
        if (index == 0) 
        {
            name.text = "Mac Logan";
            subname.text = "O coração valente";
        }
    }
}

This script is linked to a gameobject that has the characters of the game that depending on each the biography and specifications will change. This can be seen in Public void Confirmbutton.

So how do I change a text depending on the characters that are appearing?

The part of [Serializefield] up to private Text subname = null; I saw it in a tutorial but it didn’t work. inserir a descrição da imagem aqui

  • Have you tried to enable and disable text ? Ex: use the following condition in script: x is enabled the text of x, if you have the character y active shows the text of y, and so successively making the response of each character to each text activating and disabling texts and Objects.

1 answer

1

You have to pick up the text component using

Text descricao = GetComponent<Text>()

Or declare a variable

public Text descricao;

And in the editor, associate the text component that will be changed to the field. Then write it down descricao.text = "Texto a ser mudado".

Now, to put a different text for each character, you need a component in the gameobject of the character.

Create a Script with a name like "InfoPersonagem", and in it declare a string "Description" and put the text you want. Then you just put:

descricao.text = characterList[index].GetComponent<InfoPersonagem>().descricao;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.