Hi there. Im a very new coder and i cant for the life of me figure out how to set “ScoreNum” to 0 when colliding… I know how to collect the coins and count them up, but not reset… Anyone know where i should look, or what i should do?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CoinCollection : MonoBehaviour
{
public Text myscoreText;
private int ScoreNum;
// Start is called before the first frame update
void Start()
{
ScoreNum = 0;
myscoreText.text = "Coins : " + ScoreNum;
}
private void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == "MyCoin")
{
ScoreNum++;
}
if (other.tag == "Traps")
{
ScoreNum = 0;
}
}
}
Its only 31-34 im having troubles with. Setting a Debug.Log at line 33 displays a log whenever i jump into my triggers with the Trap tag. But it doenst set the ScoreNum to 0. My guess is that i cant jsut do ScoreNum = 0, but to reset the counter somehow… But i cant find anything about that. Life of a coding noob is hard.
So placing your Log inside the “Traps”-Condition does trigger it?
You can set ScoreSum to zero. Definitely. Place a log after line 33 with ScoreNum in it and you will see it has become zero. It must be changed again at a different place. Your IDE will most probably be able to show all occurences of ScoreNum (right click it to check that) and figure out where else ScoreNum is changed.
IF this is the whole script and you see in your log, that your 2nd if is reached, the only possibility is, that there is again a collision and your first if is called after setting “ScoreNum = 0”.
Just guessing, but is your problem really that ScoreNum is not 0 or is your actually problem, that the text you display (myscoreText) doesn´t show 0 ? Because you never update it. If you wanna set the correct value in the GUI, you have to update your text in both ifs:
if (anyStatement)
{
// set the ScoreNom value
myscoreText.text = "Coins : " + ScoreNum;
}
You are correct, i actually send the data to a counter on my canvas!
[SOLUTION AFTER THIS]
So i should do something in this script?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class isCoinCollect : MonoBehaviour
{
Text Coins;
public static int coinAmount;
// Start is called before the first frame update
void Start()
{
Coins = GetComponent<Text> ();
}
// Update is called once per frame
void Update()
{
Coins.text = coinAmount.ToString();
}
}