Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question change Text with script TEXT MESH PRO

Discussion in 'UGUI & TextMesh Pro' started by pablodelgrosso1, Oct 7, 2021.

  1. pablodelgrosso1

    pablodelgrosso1

    Joined:
    Feb 28, 2021
    Posts:
    1
    Hello there

    I'm testing a simple game in unity that is about changing the middle text with arrows.
    I made a TextMeshPro list to be able to change text based on the index, but it tells me this error "Assets \ Texts.cs (23,26): error CS0029: Cannot implicitly convert type 'TMPro.TextMeshProUGUI' to 'string'"

    I have 3 scripts:

    TextSwitcher corresponding to the arrow GameObject,

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5.  
    6. public class TEXTSwitcher : MonoBehaviour {
    7.     [SerializeField] private bool _isLeftArrow;
    8.     private bool _isPlayerLooking;
    9.     private Texts _Texts;
    10.  
    11.  
    12.     private void Start() {
    13.         _Texts = transform.parent.GetComponentInChildren<Texts>();
    14.         _isLeftArrow = true;
    15.     }
    16.  
    17.     private void Update() {
    18.         if (_isPlayerLooking) {
    19.             if (Input.GetMouseButtonDown(0)) {
    20.                 if (_isLeftArrow) {
    21.                      _Texts.PreviousText();
    22.                 } else {
    23.                      _Texts.NextText();
    24.                 }
    25.             }
    26.         }
    27.     }
    28.  
    29.     private void OnRaycastEnter(GameObject sender) {
    30.         if (sender.CompareTag("Player")) _isPlayerLooking = true;
    31.     }
    32.  
    33.     private void OnRaycastExit(GameObject sender) {
    34.         if (sender.CompareTag("Player")) _isPlayerLooking = false;
    35.     }
    36. }
    Texs corresponding to the TextMeshPro GameObjects,

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5.  
    6. public class Texts : MonoBehaviour
    7. {
    8.     private List<TextMeshProUGUI> _text = new List<TextMeshProUGUI>();
    9.     private TextMeshProUGUI _textoTMP;
    10.     private int _currentIndex = 0;
    11.  
    12.     private void Start() {
    13.         _text = GameManagerA._i._TMPtext;
    14.         _textoTMP = GetComponent<TextMeshProUGUI>();
    15.     }
    16.  
    17.     public void NextText() {
    18.      
    19.         if (_currentIndex >= _text.Count) {
    20.             _currentIndex = 0;
    21.         }
    22.          _currentIndex++;
    23.         _textoTMP.text = _text [_currentIndex];
    24.     }
    25.  
    26.     public void PreviousText() {
    27.      
    28.         if (_currentIndex < 0) {
    29.             _currentIndex = _text.Count - 1;
    30.         }
    31.         _currentIndex--;
    32.         _textoTMP.text = _text [_currentIndex];
    33.     }
    34. }
    35.  
    and finally a GameManager where I create the list of TexMeshPro with
    public List<TextMeshProUGUI> _TMPtext = new List<TextMeshProUGUI>();

    i am new to c #

    any solution ?

    Thank you!