- Home /
Can't access variable due to its protection level?
I know this means public versus private in most cases however, I do not see the issue here. This is a script to determine the appropriate "Classification" depending on the time and connections made. There are 4 possible ranks. From high to low, Legendary, Epic, Excellent, and Good. Each of these have a "goal" time and connections made. For example, to achieve Legendary. You need to make 10 connections, in under 12 seconds. And to achieve Epic, you must make 8 connections in 15 seconds. However, if you make 10 connections, in 13 seconds.. that is no longer qualified for Legendary since it took you too long. So you will drop down a class, to Epic. The goal is to get the most connections in the least amount of time. If the connections made and time qualify lower than Excellent, it will give you Good by default. Here is the script that I have, It seems to be working but I can't test it until I fix this error. LevelExit.CombinedScore.scoreName' is inaccessible due to its protection level.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class LevelExit : MonoBehaviour
{
public float padding;
public GUIStyle myStyle;
public string classification = "";
public int connections = 0;
public float time = 0.0f;
void Awake()
{
GameObject levelIntro = GameObject.Find("Level Introduction");
LevelIntroduction levelIntroScript = levelIntro.GetComponent<LevelIntroduction>();
int curConnections = GameLogic.getTotalConnections();
float curTime = GameLogic.getTotalTime();
CombinedScore playerScore = new CombinedScore("Player", curConnections, curTime);
/// Here we initialize all the score values.
CombinedScore legendaryScore = new CombinedScore("Legendary",
levelIntroScript.LegendConnect,
levelIntroScript.LegendTime);
CombinedScore epicScore = new CombinedScore("Epic",
levelIntroScript.EpicConnect,
levelIntroScript.EpicTime);
CombinedScore excellentScore = new CombinedScore("Excellent",
levelIntroScript.ExcellentConnect,
levelIntroScript.ExcellentTime);
CombinedScore goodScore = new CombinedScore("Good",
levelIntroScript.GoodConnect,
levelIntroScript.GoodTime);
/// Now put them all in a list, in order
List<CombinedScore> referenceScoresFromHighToLow = new List<CombinedScore>() { legendaryScore, epicScore, excellentScore, goodScore };
/// Then pass in the player's score along with the list, so it can compare them
classification = GetScoreClassification(playerScore, referenceScoresFromHighToLow);
}
public struct CombinedScore
{
int connections;
float time;
string scoreName;
public CombinedScore(string name, int conns, float t)
{
scoreName = name;
connections = conns;
time = t;
}
}
string GetScoreClassification(CombinedScore playerScore, List<CombinedScore> referenceScoresFromHighToLow)
{
foreach (CombinedScore score in referenceScoresFromHighToLow)
{
if (isBetterScore(playerScore, score))
{
/// If it is a better score it returns the value, breaking out of the loop early.
return score.scoreName;
}
}
/// The value if it doesn't beat any of the reference scores
return "Terrible";
}
bool isBetterScore(CombinedScore playerScore, CombinedScore referenceScore)
{
if (playerScore.connections >= referenceScore.connections && playerScore.time <= referenceScore.time)
{
return true;
}
return false;
}
void OnGUI()
{
//Get the number of the current level. Since the range from 0, 1, 2+
//Add 1.
int level = Application.loadedLevel + 1;
GUI.Label(new Rect((Screen.width / 2) - (125 / 2), (Screen.height / 2) - (125 / 2) + padding + 25, 125, 25), "Level " + level.ToString(), myStyle);
///If you want to update this in real time, you can call GetScoreClassification right here!
GUI.Label(new Rect((Screen.width / 2) - (125 / 2), (Screen.height / 2) - (125 / 2) + padding, 125, 25), "Classification: " + classification, myStyle);
GUI.Label(new Rect((Screen.width / 2) - (125 / 2), (Screen.height / 2) - (125 / 2) + padding - 25, 125, 25), "Connections: " + GameLogic.getTotalConnections(), myStyle);
GUI.Label(new Rect((Screen.width / 2) - (125 / 2), (Screen.height / 2) - (125 / 2) + padding - 50, 125, 25), "Time: " + (int)GameLogic.getTotalTime(), myStyle);
}
}
Answer by Bunny83 · Nov 08, 2014 at 06:27 AM
Well, your 3 variables in your "CombinedScore" struct are all private ... The default visibility in C# is private. If you want them to be accessible from outside the struct, make them public.
Answer by Kiwasi · Nov 08, 2014 at 06:28 AM
You have declared scoreName as private. (C# defaults to private if you don't specify an access modifier).
CombinedScore is a struct that is separate object from LevelExit. Hence LevelExit cannot access private variables of CombinedScore. Neither can CombinedScore access private variables of LevelExit.
Fix by declaring scoreName as public.
Almost ;) It's true that the outer class has no access to private variables of the inner class, but the inner class has access to private variables of the outer class since the innerclass is part of the scope of the outer class. A simple example:
public class OuterClass
{
private string var1 = "$$anonymous$$y private string";
public void Print()
{
Debug.Log(var1);
}
public class InnerClass
{
public void Change(OuterClass t1)
{
t1.var1 = "Not anymore";
}
}
}
var outer = new OuterClass();
var inner = new OuterClass.InnerClass();
outer.Print(); // "$$anonymous$$y private string"
inner.Change(outer);
outer.Print(); // "Not anymore"
This does compile and works without any errors or warnings.
Yeah thanks its working.. but not actually working. $$anonymous$$y script is fixed since I made the variables public. However no clue why it is always saying i did terrible. I get to the end and it says i have 6 connections in 4 seconds, which I have on the level introduction set exactly and its saying terrible still.. wth...
@Josh$$anonymous$$Beyer: Well, we don't know the values you use since they seem to be defined in another script. You do your calculations in Awake. Are you sure that you don't "create" this script too early? Awake and Start are only called once in the lifetime of the script. You might want to store the time and connections in a local variable and display those in the gui.
Your answer
![](https://koobas.hobune.stream/wayback/20220613171014im_/https://answers.unity.com/themes/thub/images/avi.jpg)