Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Error CS0122:" --- is inaccessible due to its protection level" but the bool is public

Discussion in 'Scripting' started by Lunastras, Jun 28, 2017.

  1. Lunastras

    Lunastras

    Joined:
    Jun 28, 2017
    Posts:
    6
    I am trying to access some public bools from one class to another, and I don't know what I goofed up and why it doesn't work.

    The lines that matter are 11 and 12.
    The class with the original variables is:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Misc : MonoBehaviour {
    5.  
    6.  
    7.     public int player_health=4;
    8.     public int player_HP=0;
    9.     public bool CanTakeDMG=true;
    10.  
    11.     public bool PlayerBlink=false;
    12.     public bool Blinked = false;
    13.  
    14.     playerController Face;
    15.  
    16.  
    17.     IEnumerator DmgCooldown(float time){
    18.         yield return new WaitForSeconds(time);
    19.         CanTakeDMG=true;
    20.         PlayerBlink=false;
    21.         Face = GetComponent<playerController>();
    22.     }
    23.  
    24.     public void damage(){
    25.         player_health--;
    26.         CanTakeDMG = false;
    27.         PlayerBlink = true;
    28.         Face.myRB.AddForce (Face.GetFacing () * 300f, 0f, 0f);
    29.         StartCoroutine (DmgCooldown (3f));
    30.     }
    31.  
    32.     public void makeDead (){
    33.         Destroy(gameObject);
    34.     }
    35.  
    36.     void Start () {
    37.    
    38.     }
    39.  
    40.  
    41.     // Update is called once per frame
    42.     void Update () {
    43.        
    44.         if (player_HP >= 100 && player_health < 4) {
    45.             player_HP = 0;
    46.             player_health++;
    47.         }
    48.  
    49.         if (player_health == 0) {
    50.             makeDead ();
    51.         }
    52.  
    53.  
    54.  
    55.     }
    56. }
    57.  
    And the code in which I get the errors is:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DmgBlink : MonoBehaviour {
    5.  
    6.     Misc BlinkB;
    7.  
    8.  
    9.     void Start () {
    10.        
    11.         BlinkB = GetComponent<Misc>();
    12.  
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void Update () {
    17.    
    18.         if (BlinkB.PlayerBlink)
    19.             if (BlinkB.Blinked) {
    20.                     GetComponent<Renderer> ().enabled = true;
    21.                     BlinkB.Blinked = false;
    22.                 }
    23.                        
    24.                 else{
    25.                     GetComponent<Renderer> ().enabled = false;
    26.                     BlinkB.Blinked = true;
    27.                 }
    28.                
    29.         }
    30. }
    31.  
    The exact errors are:
    Assets/DmgBlink.cs(18,28): error CS0122: 'Misc.PlayerBlink' is inaccessible due to its protection level.
    Assets/DmgBlink.cs(19,36): error CS0122: 'Misc.Blinked' is inaccessible due to its protection level.
    Assets/DmgBlink.cs(21,48): error CS0122: 'Misc.Blinked' is inaccessible due to its protection level.
    Assets/DmgBlink.cs(26,48): error CS0122: 'Misc.Blinked' is inaccessible due to its protection level.

    I also feel like pointing out that the first class is connected to an empty gameObject, while the second is attached to a plane mesh that is a child to the empty gameObject.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,148
    Well, GetComponent<Misc>() will look at the same gameobject, so it would normally give you a null error when you run it, except it thinks the variables are not accessible. Use getcomponentinparent instead

    BlinkB = gameObject.GetComponentInParent<Misc>();

    As far as your error goes, if you had the values set to private at one point, make sure you saved and that Unity recompiled the code, otherwise, it may still think it's private.

    If neither of these fixes it. Restart Unity. Otherwise, I'm not noticing an issue with your code.
     
  3. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    the simple answer is that it can't find Misc script.
    if the Misc script is not on the same gameObject as DmgBlink. the Get Component<Misc> should be null
     
  4. Lunastras

    Lunastras

    Joined:
    Jun 28, 2017
    Posts:
    6
    Godspeed! I was a bit of a fool, but I realised my script was not even attached to the plane, but after connecting it, I got the Null message, which was fixed after using your suggestion. Cheers, Brathnann.
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    A tip, turn on clear errors on play in the debugger. That way any compile errors that you fixed will be wiped before you start playing. In this case the compiler errors and your actual problem were completely unrelated, and having the extra errors there just confused things.