OnTriggerStay is reseting a float? [SOLVED]

When an object enters a cube, a ‘recoveredOil’ counter is multiplied by a ‘production’ counter. This is the script:

void OnTriggerStay(Collider other)
{
GameObject gameManagementObject = GameObject.Find(“GameManagementObject”);
GameManagement gameManagement = gameManagementObject.GetComponent();
gameManagement.recoveredOil = gameManagement.recoveredOil + (production * Time.deltaTime);
}
}

The ‘recoveredOil’ counter does in fact change but every time the collider exits the cube and re-enters it, the counter is set to 0 again before starting to change.
What am I doing wrong?

UPDATE: I restarted Unity and the script simply worked.

It could be many things. I would double check how you’re instantiating your “recoveredOil” variable. Is the GameManagement script being disabled/enabled again somehow and this is resetting the variable?

The code you’ve posted should work, although, why are you multiplying the production by deltaTime?

the ‘production’ is a gameplay variable, but regarding the script, it simply worked after I restarted Unity. I call this move the ‘‘I.T. Crowd’’.

1 Like