- Home /
Random increase of int in PlayerPrefs
(Sorry for bad knowledge of English)I have some problem with code. I write a script that will increase souls value by1, when the Player collider2d enter in soul collider2d(trigger).
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == "Die")
{
lose = true;
}
if (col.gameObject.tag == "Soul")
{
PlayerPrefs.SetInt("Soul", souls + 1);
Debug.Log(PlayerPrefs.GetInt("Soul"));
}
}
Thes Code must increase souls by 1 but Rarely suls increse by 2. I don't know whats wrong with it. Can it be becouse player have BoxCollider2D, CircleCollider2D&`void FixedUpdate () {
//Souls
souls = PlayerPrefs.GetInt("Soul");
if (souls >= 3)
{
PowerAttack.SetActive(true);
}
else if (souls <= 3)
{
PowerAttack.SetActive(false);
}
//----------`
In fact, yes. Having 2 colliders will make the trigger function sometimes call twice.
In fact, yes. Having 2 colliders will make the trigger function sometimes call twice.
Not doubting this, just asking - do you maybe have a reference for that? Seems like unwanted behavior i.e. a bug to me.
I think it's perfectly valid behavior. To get around it, either remove one collider or immediately change the behavior of the soul object. For example:
if (col.gameObject.tag == "Soul")
{
PlayerPrefs.SetInt("Soul", souls + 1);
col.gamObject.tag="NoLongerASoul";
Debug.Log(PlayerPrefs.GetInt("Soul"));
}
To make it so it can't get called twice.
Your answer
Follow this Question
Related Questions
My ground checker only works... sometimes 1 Answer
Problem with Falling Platform Script 3 Answers
"Dash-Move" only work some times. 1 Answer
Play and Stop Animation 1 Answer
Softbody physics with 2D sprites 1 Answer