Programmier Fehler kann mir wer helfen?

 if (other.tag == "Coin")
  {
    coinCounter++;
    Destroy (other.gameObject);
    scoreText = "Score: " + coinCounter;
    Debug.Log ("Score: " + coinCounter);
  }

Das Ist mein Code und irgendetwas ist daran nicht richtig den ich kriege immer diesen error Assets\scripts\Player.cs(26,21): error CS0029: Cannot implicitly convert type ‘string’ to ‘UnityEngine.UI.Text’ kann mir wer helfen?

(2 votes)
Loading...

Similar Posts

Subscribe
Notify of
6 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
GuteAntwort2021
1 year ago

Hello.

The error states that the assignment

scoreText = "Score: " + coinCounter;

can’t work like that. I’ve never done anything with Unity, but probably there’s a method to set texts, or it’s ideally an object, then there’s probably gonna be the field “Text” you can set.

Try it like that.

scoreText.text = "Score: " + coinCounter;

and if that doesn’t work, then so:

scoreText.SetText("Score: " + coinCounter);

Good luck. 👍

GuteAntwort2021
1 year ago
Reply to  SleimieLP79

Like I said, I never did anything with Unity. I can only make guesses. But it’s nice that the other one worked! 👍

Xandros0506
1 year ago
Reply to  SleimieLP79

and if you want to do it correctly, you will not only flip a variable with a numerical value into the text attribute, but you will convert it properly into a string.

scoreText.text = "Score: " +  coinCounter.ToString();
SoIid
1 year ago
using UnityEngine;
using UnityEngine.UI;

public class Player : MonoBehaviour
{
    public int coinCounter = 0;
    public Text scoreText;

    void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Coin")
        {
            coinCounter++;
            Destroy(other.gameObject);
            scoreText.text = "Score: " + coinCounter;
            Debug.Log("Score: " + coinCounter);
        }
    }
}

Make sure this variable is set in the Unity Inspector:

public UnityEngine.UI.Text scoreText;