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

C# inaccessible due to its protection level

Discussion in 'Scripting' started by PeterR, Jul 15, 2014.

  1. PeterR

    PeterR

    Joined:
    Jul 15, 2014
    Posts:
    15
    I get the following error when my code compiles:

    The line it's referring to is the constructor for a class:

    Code (csharp):
    1. public InventorySlot(int ID, Item item=new Item(), int count=0, ItemDatabase data=null, SlotType type = SlotType.normal, Rect rect=new Rect(), GUISkin input_skin=null, string style="inventory_slot")
    It's taking issue with this part:

    Code (csharp):
    1. Item item=new Item()
    but my Item class and it's blank constructor are public

    Code (csharp):
    1. [System.Serializable]
    2. public class Item {
    3. ...
    4. public Item()
    5.     {
    6.     }
    Any ideas?
     
  2. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Two classed named "Item"?
     
  3. PeterR

    PeterR

    Joined:
    Jul 15, 2014
    Posts:
    15
    I am only defining one class, this line:
    Code (csharp):
    1. public Item()
    is just defining a constructor for that class.
     
  4. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Code (csharp):
    1. public InventorySlot(int ID, Item item=new Item(), int count=0, ItemDatabase data=null, SlotType type = SlotType.normal, Rect rect=new Rect(), GUISkin input_skin=null, string style="inventory_slot")
    2.  
    Default parameter values were introduced in .net 4. Im not sure what version of .net current Unity has, but I suspect this is not supported.
     
  5. PeterR

    PeterR

    Joined:
    Jul 15, 2014
    Posts:
    15
    Thanks for the reply James. I did some searching and it seems that unity's implementation of .NET does support default parameter values (thank god).

    To confirm, I tried this (which compiles without any errors, it just doesn't work for my code):
    Code (csharp):
    1. Item item=null
     
  6. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    well if its null, you can initialise the item within your constructor, so no biggie.
     
  7. PeterR

    PeterR

    Joined:
    Jul 15, 2014
    Posts:
    15
    Yea, that's what I'll do I suppose. It would be good to know what the source of the problem is though.
    Thanks!
     
  8. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Having never used default parameters in c#, not sure what the limitation is, but I suspect you cant default new classes.

    Maybe ill have a play in non-unity code today and see what happens
     
  9. PeterR

    PeterR

    Joined:
    Jul 15, 2014
    Posts:
    15
    That was my initial thought, but Unity has no problem with this part of that line
    Code (csharp):
    1. Rect rect = new Rect()
     
  10. Lendis12

    Lendis12

    Joined:
    Jun 13, 2018
    Posts:
    3
    I keep getting sort of the same error: 'PlayerController.GetThrusterFuelAmount()' is inaccessible due to its protection level [Assembly-CSharp]. Please help me
     
  11. Fajlworks

    Fajlworks

    Joined:
    Sep 8, 2014
    Posts:
    344
    try using the default(T) in the method signature

    Code (CSharp):
    1. public InventorySlot(int ID, Item item=default(Item), int count=0, ItemDatabase data=null, SlotType type = SlotType.normal, Rect rect=default(Rect), GUISkin input_skin=null, string style="inventory_slot")
    Also, having too many arguments is regarded as a code smell, especially when the null ones aren't put at the back of the argument list. Ideally, you want to have only the necessary arguments that are required for the class to function normally. That will allow another developer that is not familiar with your code to automatically understand the necessary variables and to make fewer mistakes initializing the object (like having to call Initialize method after the instantiation). For additional customization you might opt into the factory design approach.
     
  12. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    All members of C# classes (and structs) have a protection (or accessibility) level. You can read more about that here. The level can be given either explicitly or implicitly.

    Your error is saying that, whilst
    GetThrusterFuelAmount
    does exist in the target (
    PlayerController
    ), the caller does not have the right (sufficient permission) to call that method.

    Just to check, have you covered any basic C# / Unity tutorials to get you started? It could be worth it. You can scroll to the top of this page and click on "Learn". :)
     
  13. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Just to mention, it might be worth noting that the OP in this thread was 4 years ago. I presume they have solved their original problem by now. :)
     
    Fajlworks likes this.
  14. Fajlworks

    Fajlworks

    Joined:
    Sep 8, 2014
    Posts:
    344
    LOL, I totally overlooked that part, thanks for pointing that out! Hopefully, it might help someone else who has the same issue...
     
    Doug_B likes this.