so i'm trying to increase my score every time i hit a wall (IsTrigger) but when i try to increase it, it doesn't work and still 100
so this is the code im having problem with
public Text scoreText;
public int score = 0;
void Start()
{
scoreText.text = score.ToString();
}
// Add Some Score To The Text When The Player Hit The CheckPoint
void OnTriggerEnter(Collider collider)
{
if (collider.name == "Player")
{
score = 100;
scoreText.text = score.ToString();
Debug.Log(score);
}
}
CodePudding user response:
When doing the trigger/collision test, make sure you add one
Debug.Log()
outside of the condition check. In your case,
void OnTriggerEnter(Collider collider)
{
if (collider.name == "Player")
{
score = 100;
scoreText.text = score.ToString();
Debug.Log(score);
}
Debug.Log("Just want make sure it indeed triggered.");
}
In most case, other than the issue with the code, it is highly possible the Rigidbody component is not added to the GO.
Another part which I am not sure since I am not using Unity for quite a long time now. You can move the UI text update code outside of the trigger if you want.
CodePudding user response:
You need to update the text after changing the value, in your case you have to update "scoreText" according to the new "score" value. The simple way to do it is like that:
public Text scoreText;
public int score = 0;
void Start()
{
scoreText.text = score.ToString();
}
// Add Some Score To The Text When The Player Hit The CheckPoint
void OnTriggerEnter(Collider collider)
{
if (collider.name == "Player")
{
score = 100;
scoreText.text = score.ToString();
Debug.Log(score);
}
}
It is better to have a method called "ChangeScore" which includes updating the "score" value and updating the "scoreText" at the same time. It is better to do so since you might forget to update the text in other cases if you have to change the score somewhere else. Example:
[Tooltip("The player's initial score.")]
[SerializeField] private int initScore = 0;
// Serialized since you have to set the reference for it.
[SerializeField] private Text scoreText;
private int score = 0;
void Start()
{
ChangeScore(initScore);
}
// Method used to change the current player's score and update the text.
void ChangeScore(int changeValue)
{
this.score = changeValue;
scoreText.text = score.ToString();
}
// Add Some Score To The Text When The Player Hit The CheckPoint
void OnTriggerEnter(Collider collider)
{
if (collider.gameObject.name == "Player")
{
ChangeScore(100);
Debug.Log(score);
}
}
Another side note: I wouldn't use tags or names while checking colliding, its better to check if the colliding object got the PlayerController script
if(collider.GetComponent<PlayerController>())
instead of
if (collider.gameObject.name == "Player")
CodePudding user response:
i fixed it i put the code on the Player script not on the CheckPoint script
void OnTriggerEnter(Collider collider)
{
if (collider.tag == "ScoreCheckPoint")
{
score = 100;
scoreText.text = score.ToString();
Debug.Log(score);
}
}