Applied this principle to create a Score Multiplier

…but not getting expected return value.

I think this video helped my issue with creating a multiplier attached to the Block script to communicate with the score keeper. I had initially over-complicated the method by passing parameters back and forth when I just needed to return the multiplier by itself.

However, no matter what I enter in the Serialize Field for “float multiplier,” it returns as 1 according to the Debug.Log I set up in my GetMultiplier() method on the Block script. I even tried hard-coding the multiplier to 10f and it returns 1.

In Block.cs:

[SerializeField] float multiplier;
...
...
public float GetMultipler()
{
     Debug.Log(multiplier);
     return multiplier;
}

And in GameStatus.cs:

...
...
public void AddToScore()
{
     currentScore = currentScore + (pointsPerBlock * block.GetMultiplier())
}

So, my guess as to why this is happening is because when I use FindObjectOfType, the method is deriving the multiplier from the first block it finds and not on each particular block that’s being destroyed and adding to the score.

If that is the case I might need to derive a new scorekeeping methodology to track it.

Hi,

The FindObjectOfType method returns the first object of a type it finds, whatever Unity defines as “first”. If you need a specific object and know more about it, e.g. to which game object it is attached, use GetComponent on that GameObject object.

Your code snippets look fine, so if something does not work as expected, it is very likely an issue with a reference.

1 Like

Thanks for confirming! I was kind of rubber ducking my way through the problem with this post, but I’m glad I ended up somewhere near the right conclusion.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms