For some reason I cannot get this simple script to plug into my text box. When I try and add it to the text box through the hierarchy panel, it tells me that the class cannot be found and if I create a script through the add component area, nothing shows of it other than it has a script on it, nothing when you click on the drop down arrow. The script should allow a button component to add 1 or subtract one point to the score box. The file is saved as Score.cs.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Score : MonoBehaviour
{
public Text score;
public int scoreAmount;
// Start is called before the first frame update
void Start()
{
scoreAmount = 0;
score = GetComponent<Text>();
}
void Update()
{
score.text = scoreAmount.ToString();
}
public void AddScore()
{
scoreAmount += 1;
}
public void SubtractScore()
{
scoreAmount -= 1;
}
}
because a text box is not of type Text usually
Are you sure the Score.cs file is not inside an Editor folder?
That particular attempt that was Score.cs was stored in a sub script folder in the assets folder that I created. I just tried creating one through the text component and I thought maybe it is because it is TMPro and you cannot just call on text like they did in tutorials from years ago. So I tried this, and I am still getting the same issue. Nothing shows when I drop the arrow down on the script on the text component.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class NewScore : MonoBehaviour
{
public TMP_Text score;
public int scoreAmount;
// Start is called before the first frame update
void Start()
{
scoreAmount = 0;
score = GetComponent<TMP_Text>();
}
void Update()
{
score.text = scoreAmount.ToString();
}
public void AddScore()
{
scoreAmount += 1;
}
public void SubtractScore()
{
scoreAmount -= 1;
}
}
If nothing is showing on the component in the inspector, it usually means you have some kind of compilation error preventing Unity from commencing serialisation.
You need to make sure your class names and script files match exactly, too.
I know, there seems to be no compiling error, no red marks or anything like that. It is odd.
I saw it on a tutorial and it worked for them. But, that was before TMPro, I tried resolving it like that in another reply I made, but that did not seem to do anything.
Can you just post a screenshot of the editor and error?
Well, the issue is different now. I think my project might be bugged. I pulled up a test project file by creating a new project and then tried the same thing and the scripting added onto the text, but it does not work like it shows in this tutorial. I am just learning trying to do my own thing and am starting with basic achievements and trying to sculpt them into something more intricate, and this was an area of things that I needed to conquer. So, any help is appreciated. Both tutorials use the same method. One would think it should still work.
I got it working! It is because it has to be called out in TMPText like in the updated script I posted and for some reason project is corrupted and I think I just have to import my scenes and needed files into a clean enviornment.