Search Unity

error CS0029: Cannot implicitly convert type `UnityEngine.GameObject[]' to `UnityEngine.UI.Text'

Discussion in 'Scripting' started by ryukia, Jun 22, 2018.

  1. ryukia

    ryukia

    Joined:
    Feb 12, 2018
    Posts:
    17
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class ScoreTracker : MonoBehaviour
    7. {
    8.  
    9.     static Text scoreText;
    10.     static Text messageText;
    11.     static int currentScore = 0;
    12.     static string currentMessage = "Lets play!";
    13.  
    14.     void Start()
    15.     {
    16.         scoreText = GameObject.FindGameObjectsWithTag ("ScoreText").GetComponent<Text>();;
    17.         messageText = GameObject.FindGameObjectsWithTag ("MessageText").GetComponent<Text>();;
    18.         UpdateScore (currentScore);
    19.         UpdateMessage (currentMessage);
    20.  
    21.     }
    22.  
    23.     public static void UpdateScore(int addedValue)
    24.     {
    25.         currentScore += addedValue;
    26.         scoreText.text = "" + currentScore;
    27.     }
    28.  
    29.     public static void UpdateMessage (string message)
    30.     {
    31.         currentMessage = message;
    32.         messageText.text = currentMessage;
    33.     }
    34. }
    35.  
    error CS1061: Type `UnityEngine.GameObject[]' does not contain a definition for `GetComponent' and no extension method `GetComponent' of type `UnityEngine.GameObject[]' could be found. Are you missing an assembly reference?

    come out at GetComponent.
     
  2. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    ryukia likes this.
  3. ryukia

    ryukia

    Joined:
    Feb 12, 2018
    Posts:
    17
    it works. thank you