last day (18 days later) » 

18:55
0
Q: Increase score when object is destroyed

Bailey Mc JarrowSo all I want to do is to increase my score by one when an object is destroyed. The objects are destroying but my score is not increasing... Heres my code: public Transform obstacle; public GameObject Obstacle; public Text Score; public int counter; // Start is called before the first frame updat...

Are you sure you are actually using "Score" somewhere?
you are testing on the transform obstacle but you are destroying GameObject Obstacle, it's confusing. Is obstacle always Obstacle.transform ?
thank you for the response but it didnt work. and yes, i say "score.text"
yes, obstacle is Obstacle.transform
I mean, do you use Score.text somewhere in your scene / UI? If you just add Debug.log(Score.text); after Score.text = counter.ToString();, does it show the correct value?
Yes, I have a UI with a text but it doesnt change, stays at 0
and what about the debug log?
18:56
nothing in debug.log, should i do that just to see if its a code problem or a UI problem
ok, so i just did a debug.log and it just prints 0. so its something with the code, not UI
exactly.

On which object is the script that you showed us?
player and obstacle prefab
ah, then I can see a problem here. If you have this script on several instances, then they have each their own copy of all the fields (counter, and Score)
What I suspect is that counter is updated in the script of each object, and in the UI what is shown is the counter of the player (which stays always at zero).

One simple solution would be to make counter a `static` field, so it will be the same value shared among all the instances.
okay, i will try
still nothing
19:13
let's do another check (to properly debug, we need to understand which exact code is executed):

Try to add a `Debug.log` _inside_ the `if` block
jsut to show some message like "hello", and see if this part is actually executed.
It worked!! I was using the new method i had created outside of my update if-statement.
Thank you so so so much for your help!!!!
ok, glad to help!

  last day (18 days later) »