- Home /
Score System - Problem found - Need help with solution
NOT FIXED, but I may have found the problem. I tried changing the way it works slightly, so that the only thing that EnemyKill() passes, is the incoming score to a temporary variable, scoreToIncrease -- w/ debug logs to show what it's actually doing.
-- Debug.Log("Update - scoreToIncrease: " + scoreToIncrease);//in Update() - always shows zero.
-- Debug.Log("EK() scoreToIncrease: " + scoreToIncrease);//in EnemyKill() - shows the correct value.
When the incoming function is called, it is changing/creating a new(?) variable, but the class variable is not being affected. Adjusted code.(Shortened)
public static float scoreMultiplier;
private float multiplierFallOff = 2f;
public float scoreTimer = 0f;
public float scoreMultiplierTemp;
public float scoreToIncrease = 0f;
void Update ()
{
Debug.Log("Update - scoreToIncrease: " + scoreToIncrease);//- Always 0
if (scoreToIncrease > 0)
{
scoreTimer += Time.timeSinceLevelLoad + multiplierFallOff;
gameManager.playerScore += (int)((scoreMultiplier * scoreToIncrease) + scoreToIncrease);
scoreMultiplier += 0.1f;
scoreMultiplierTemp = scoreMultiplier;
scoreToIncrease = 0;
}
}
public void EnemyKill(int score)
{
Debug.Log("Inc Score: " + score);
scoreToIncrease = score;
Debug.Log("EK() scoreToIncrease: " + scoreToIncrease);//- Shows correct value
}
}
Answer by Tourist · Nov 10, 2017 at 09:21 AM
scoreTimer is a public variable. Since public are serialized by default in the editor, maybe you have a value in the inspector that is serialized.
If that so, either add [NonSerialized] before the field or put it to private. You can also avoid setting values in the field declaration and put them in an Awake function. As it would fix the issue, it would hide the fact that your field is serialized for no reason.
As it is a timer, I don't change those values in the inspector / other scripts-- one of my bug squashing ideas - was that somehow, that had happened - so I deleted the original timer variable and remade a new one - since that would initialize it in w/e unseen inspector - sadly new variable showed same behavior. It's in the script itself. - I also attempted ctrl+f - search in solution, scoreTimer I can guarantee this is the only script that affects it
As far as I'm aware, the only point to non-serializing variables, is when you're working with a $$anonymous$$m, and you don't name them in a way that says "Don't you touch it". I'm not currently working with a $$anonymous$$m - all solo dev up in heyah - and being able to see variable values - really helps when finding bugs.
I mean, if serializing is bad, I'm going to have all sorts of problems w/ the [System.Serializable] stuff I did
Don't serialize it, put it to private. You may be able to view it on the editor using the Inspector Debug mode (top right corner).
I usually only make variables I need to be able to see in the inspector - public -- like now - while attempting to find out why the code is not functioning as intended. Using System.Serializable - simply organizes such variables for view. I don't know why there isn't something like [Header("Settings")] that also has a drop-down --- but from my research on the subject -- a variable being serializable doesn't make it more "expensive" to use or have any real negative affects - unless you're working in a $$anonymous$$m environment -- and in that $$anonymous$$m environment - someone changed a variable they shouldn't have - an easy thing to fix.
Perhaps you have the pro version of unity & your debug view is fancier than $$anonymous$$e, but when I use it, it doesn't show private variables or static variables -- making it functionally useless to me.
Answer by dalessan9 · Nov 10, 2017 at 12:34 PM
Edit** Comment removed to update initial post w/ new info.
Your answer
Follow this Question
Related Questions
Unity can't use the script. 0 Answers
How to give score only once for each level? 0 Answers
How to alter c# script from a script in a different scene (i.e. display score in different scene) 2 Answers
Multiple Cars not working 1 Answer
Which mistake could lead to the detection of a non-existing GameObject? 1 Answer