Script updates prefab text
I have a prefab called score that contains a text mesh. That prefab has a script attached, also called score.
When i instantiate the prefab, the script starts by setting the text on the screen to "0 - 0" (or whatever I want) and it works fine. When someone scores and I call a function to increase the score and update the text nothing happens on the screen. Instead, the text of the prefab that I have on my assets gets updated.
I have debugged and confirmed that the function is being called and the variables that keep the score are being updated. It's the exact same code that I use on the Start function so I don't get why it doesn't work on a function.
Score Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Score : MonoBehaviour {
int LeftScore = 0; //initiate scores
int RightScore = 0;
void Start() {
DontDestroyOnLoad(this); //in order to keep the score when a new game starts
transform.position = new Vector2(0, GameManager.topRight.y); //center, top of the screen
GetComponent<TextMesh>().text = LeftScore.ToString() + " - " + RightScore.ToString(); //writes the score
}
public void UpScore(bool DidRightScore) {
if (DidRightScore) { //increments the score
RightScore++;
} else {
LeftScore++;
}
GetComponent<TextMesh>().text = LeftScore.ToString() + " - " + RightScore.ToString(); //writes the score
}
}
Script that calls the Score script:
public Score scoretext;
....
void Update() {
....
if (transform.position.x < GameManager.bottomLeft.x + radius && direction.x < 0) {
scoretext.UpScore(true);
}
if (transform.position.x > GameManager.topRight.x - radius && direction.x > 0) {
scoretext.UpScore(false);
}
}
(Im having trouble uploading the images of the inspector so I posted them here https://imgur.com/a/mzoVFeY)