Hello! First thing to make everyone aware, I am still learning game development. I am in college doing it. Anyway, I am working on a final project and I seem to be having some issues with the HUD. I am trying to set two scores for the HUD (one for enemy when you kill one you get points and then collectibles) and I am following the same thing that my instructor has done and I managed to get a few issues fixed on my own…but I am having the below issue, why? Error and code below.
Error:
Assets\Scripts\Connector.cs(26,20): error CS1061: ‘int’ does not contain a definition for ‘text’ and no accessible extension method ‘text’ accepting a first argument of type ‘int’ could be found (are you missing a using directive or an assembly reference?)
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class Connector : MonoBehaviour
{
public Slider healthBar;
public Text enemyScore;
public Text collectionScore;
// Start is called before the first frame update
void Start()
{
healthBar.value = GameManager.Instance.health;
enemyScore.text = “0”;
collectionScore.text = “0”;
}
// Update is called once per frame
void Update()
{
int enemyScore = GameManager.Instance.GetComponent().enemyScore; (not sure if it has to be .enemy
int collectionScore = GameManager.Instance.GetComponent().collectionScore;
enemyScore.text = enemyScore.ToString();
collectionScore.text = collectionScore.ToString();
healthBar.value = GameManager.Instance.health;
}
}
It is having an issue with “enemyScore.text” and “collectionScore.text”.
If I delete the .text, it gives me an issue stating it cannot implicitly convert type ‘string’ to ‘int’
You should use code tags for your code.
var like = this;
It would make your snippet much more readable and I wouldn’t have to count the rows to get to the error on line 26 …
All in all, you messed big time, it’s clear as day you don’t really know what you’re doing.
So let’s start from the beginning.
-
Your class is called ‘Connector’. It connects what exactly? This is just a question, not a problem in itself. You can probably choose a better name for this script.
-
You declare two field variables enemyScore
and collectionScore
and these are of type Text
, so I’m guessing it’s the visible part of your GUI with which you actually present the score values.
-
However, there are no values themselves.
-
In Update, you suddenly declare another local variable with the same name of enemyScore
, and this has consequences. Namely, it hides the already existing class field because it has the same name. And then you assign to this variable some value that you fetch from a component on another object, via GameManager
singleton. I can’t tell if this is correct.
-
Now enemyScore.text
probably can’t work, because the local variable enemyScore
is of type int
and has no text
property.
-
For a similar reason, collectionScore.ToString()
probably doesn’t return anything meaningful, because collectionScore
is a class field which is of type Text
, so it probably only returns the word “Text”.
Pretty bad all around
1/10
But we’re here to help. However, there is not much else I can help you with.
If you don’t understand your errors, all I can say is that you need to achieve a better command over programming.
Really the basics; what is left and what is right. How variables work, how scopes work, the classes, the objects, fundamentals of OOP, as well as the nominal C# syntax … If you knew that, you would understand exactly what your instructor did and why it worked.
Feel free to ask something specific if that helps you get a better grasp on this problem.