Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by JoshMBeyer · Nov 08, 2014 at 06:19 AM · c#errorlevelprotection

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);
 
     }
 
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Nov 08, 2014 at 06:42 AM 1
Share

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.

avatar image JoshMBeyer · Nov 08, 2014 at 06:52 AM 0
Share

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...

avatar image Bunny83 · Nov 08, 2014 at 06:59 AM 0
Share

@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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

29 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Getting the error "is inaccessible due to it's protection level" when I have the function set to public. 1 Answer

Multiple Cars not working 1 Answer

not all lines of code running c# 0 Answers

Selecting Object From Top Causes NullReference 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges