Good day. The game has a GameScore class, in which there is a certain number of nested classes that must store data for comparisons. Here is the class itself:

public class GameScore : MonoBehaviour { //first level info public class FirstLevel { internal const float HightScore = 7f; internal const float MiddleScore = 11f; internal const float LowScore = 15f; internal static int bestResult = 0; } //second level info public class SecondLevel { internal const float HightScore = 5f; internal const float MiddleScore = 7f; internal const float LowScore = 9f; internal static int bestResult = 0; } } 

And now the question itself: how can I get access to the FirstLevel nested class from another class, for example?

PS I'm trying to add GameScore to another public class and from there to get access to the nested class, but nothing happens.

Update (as I try to access the FirstLevel class):

 public GameScore GameScorePrefab; private void CheckStars() { int bestResult = GameScorePrefab.FirstLevel.bestResult; //другой код } 

After that he writes: "GameScore. FirstLevel is a type, you can’t get GameScore. FirstLevel instead." And Intel class FirstLevel does not see at all.

  • How exactly does not work? And what exactly does not work? That writes? Show yourself how you write - Alexey Shimansky
  • @ Alexey Shimansky updated the question. - Pepsi4

3 answers 3

If I were you, I would have made public variables in GameScore and accessed through them. And perhaps instead of using internal public , since you might be trying to access from another namespace

 public class GameScore : MonoBehaviour { public FirstLevel firstLevel; public SecondLevel secondLevel; //first level info public class FirstLevel { internal const float HightScore = 7f; internal const float MiddleScore = 11f; internal const float LowScore = 15f; public /*static*/ int bestResult = 0; } //second level info public class SecondLevel { internal const float HightScore = 5f; internal const float MiddleScore = 7f; internal const float LowScore = 9f; public /*static*/ int bestResult = 0; } } 

then access will be as follows:

 public GameScore GameScorePrefab; private void CheckStars() { int bestResult = GameScorePrefab.firstLevel.bestResult; //другой код } 
  • public FirstLevel firstLevel = new FirstLevel(); - Igor
  • Something I did not understand the meaning of these public variables. If in the end, in order to declare them, you also have to contact through the point through the main class. so to write public GameScore GameScore; GameScore = new GameScore(); GameScore.firstLevel = new GameScore.FirstLevel(); public GameScore GameScore; GameScore = new GameScore(); GameScore.firstLevel = new GameScore.FirstLevel(); so: GameScore.FirstLevel firstLevel = new GameScore.FirstLevel(); . GameScore.FirstLevel firstLevel = new GameScore.FirstLevel(); - how I don’t understand the winning?) - Alexey Shimansky
  • Well, I understand that a person needs copies and not statics as originally written, then we make copies. And the fact that there is no sense in nested classes is yes - KingPeas

If the nested type must be applied outside the inclusive type, it must be qualified by the name of the inclusive type.

That is, if there is a class:

 public class OuterClass { public class PublicInnerClass {} private class PrivatelnnerClass {} } 

That appeal should be:

 void MyMethod() { // Создать и использовать открытый вложенный класс. OuterClass.PublicInnerClass inner; inner = new OuterClass.PublicInnerClass(); // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - полное имя } 

or

 OuterClass.PublicInnerClass inner; void MyMethod() { inner = new OuterClass.PublicInnerClass(); // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - полное имя } 

etc.


If you want to save data for each lvl, then why not use some dictionary, where the key will be the lvl number? Yes, or just add to the list. And this list is stored in asset , for example.

  • Or so, yes. The idea itself, of course, is not very rational for me. - Pepsi4
  • @ Pepsi4 but I didn’t understand it)) Why are there different values ​​in both inner classes? What are they related to? If data for each player is stored there for each level, then there is no sense in the heaps of these internal classes. one is enough. which will be stored in the list and added there, as I already wrote. And if not, then I do not understand what it is) ................... and even more so if the levels are not one and two but 10-20, even the KingPeas solution is not will save. will be like a strong crutch look .. although I did not understand the meaning of his manipulations. They are the same as my answer in the end O_o - Alexey Shimansky

Add to the code:

 using static YourNamespace.GameScore; 

and use:

 int bestResult = FirstLevel.bestResult; 

or through the name of the "external" class:

 int bestResult = GameScore.FirstLevel.bestResult; 
  • Alas, but I can add neimspaces to user static only from version 6 of c #. On Unity version 5.5, I now have c # 4.0. - Pepsi4