question

Mike Chen avatar image
Mike Chen asked

Unity: the name 'eachStat' does not exist in current context

Hi,

I'm following the example learning the get statistic function. The error message pops out: "the name 'eachStat' does not exist in current context." The switch state's eachStat cannot refer to var eachStat. What am I missing here.

void GetStats() {

PlayFabClientAPI.GetPlayerStatistics( new GetPlayerStatisticsRequest(), OnGetStats, error => Debug.LogError(error.GenerateErrorReport()) );

}

void OnGetStats(GetPlayerStatisticsResult result) {

Debug.Log("Received the following Statistics:");

foreach (var eachStat in result.Statistics)

Debug.Log("Statistic (" + eachStat.StatisticName + "): " + eachStat.Value);

switch(eachStat.StatisticName) { case "playerLevel": playerLevel = eachStat.Value; break; case "gameLevel": gameLevel = eachStat.Value; break; case "playerHealth": playerHealth = eachStat.Value; break; case "playerDamage": playerDamage = eachStat.Value; break; case "playerhighScore": playerhighScore = eachStat.Value; break; }

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Turner avatar image
Turner answered

Hey Mike,

If that is your copy pasted code then eachStat would not work in the switch because there is no brackets after the foreach. It should look something like this:

I just formatted your code and added the brackets but I haven't set up an environment to test it. Let me know if this works for you and we can go from there!

void OnGetStats(GetPlayerStatisticsResult result) 
{
	Debug.Log("Received the following Statistics:");

	foreach (var eachStat in result.Statistics)
	{//<---- *******this is what you are missing**********
	    Debug.Log("Statistic (" + eachStat.StatisticName + "): " + eachStat.Value);
 	    
	    switch(eachStat.StatisticName) 
            { 
                case "playerLevel": playerLevel = eachStat.Value; 
                   break; 

                case "gameLevel": gameLevel = eachStat.Value; 
                   break; 
              
                case "playerHealth": playerHealth = eachStat.Value; 
                   break; 

                case "playerDamage": playerDamage = eachStat.Value;
                   break; 

                case "playerhighScore": playerhighScore = eachStat.Value; 
                   break;
	    }
       }//<----
}<br>
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Mike Chen avatar image Mike Chen commented ·

Thank you Turner,

I'm sorry I didn't format my codes due to I wasn't too familiar with playfab community's system. Thank you for your quick reply.

0 Likes 0 ·
Turner avatar image Turner Mike Chen commented ·

Oh that's alright, I'm glad we were able to get you going!

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.