Search Unity

Help Needed

Discussion in 'Scripting' started by xwarflecx, May 29, 2018.

  1. xwarflecx

    xwarflecx

    Joined:
    Sep 28, 2017
    Posts:
    2
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6.  
    7. public class EntireGameManager : MonoBehaviour {
    8.  
    9.     public static int viewinglistitem = 0;
    10.     public List<string> showingtext = new List<string>();
    11.  
    12.     public GameObject thetext;
    13.  
    14.     void atl (string adding){
    15.         showingtext.Add (adding);
    16.     }
    17.  
    18.     // Use this for initialization
    19.     void Start () {
    20.         showingtext.Add ("I love chicken boiis");
    21.         showingtext.Add ("I love chicken boisssis");
    22.         atl ("AND CHICKEN IS ALSO SUPER GOOD SO YEAH ");
    23.  
    24.     }
    25.    
    26.     // Update is called once per frame
    27.     void Update () {
    28.  
    29.         string textofitem = showingtext[viewinglistitem];
    30.         GetComponent<Text>().text = textofitem;
    31.  
    32.  
    33.         if (Input.GetKeyDown(KeyCode.Space)){
    34.             if (showingtext.Count - 1 > viewinglistitem) {
    35.                 viewinglistitem += 1;
    36.             } else {
    37.                 Debug.Log("Last item of list reached");
    38.             }
    39.         }
    40.     }
    41. }
    42.  
    on line 30 i dont know and i cant find out how to get the component of the "thetext" gameobject in line 12.
    does anyone know how to do this???
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Make it thetext.GetComponent<....

    You could also make thetext directly point to the Text component, and not need the GetComponent call at all:
    Code (csharp):
    1. public Text thetext;
    2. ....
    3. thetext.text = "Foo";
     
    xwarflecx likes this.
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Another option could be, if you're not really using it as a GameObject (or less often than as a 'Text' type), simply change the variable type to Text, and drag n drop it again in the inspector. :)