Question :
I have a problem with unity3d
and I do not know how to solve it.
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 depending on each one the biography and specifications will change. This can be seen in the Public void COnfirmButton.
So how do I change a text depending on the characters that are appearing?
The part of [SerializeField] to private Text subname = null
; I saw it in a tutorial but it did not work out.
Answer :
You have to get 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 just write 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 declare a “description” string and enter the text you want.
Then just put:
descricao.text = characterList[index].GetComponent<InfoPersonagem>().descricao
;