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

How to access non public methods from a extended inherited classes?

Discussion in 'Scripting' started by frekiidoochmanz, Feb 20, 2016.

  1. frekiidoochmanz

    frekiidoochmanz

    Joined:
    Feb 7, 2016
    Posts:
    158
    I didnt realize you can only access methods derived from the next class over.
     
  2. SlyRipper

    SlyRipper

    Joined:
    Jun 19, 2012
    Posts:
    251
    so you want to access the next class after your derived class? class <- class <- class (so this one?)
    if so, you have to write adapter methods that link you through all classes.. so the first class can access the method via base.method, this method should exist and also call a base.method to the next derived class and so on. You need to pass everything through your classes till you reach the desired one.
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,376
    Access modifiers for members:
    https://msdn.microsoft.com/en-us/library/wxh6fsc7.aspx

    private - accessible only from the scope of the containing class/type

    protected - accessible only from the scope of the containing class/type, or a class/type that inherits from the containing class/type.

    internal - accessible from the containing assembly

    protected internal - accessible from the containing assembly as well as inheriting class. An assembly may use this to allow assembly wide access, but restrict access when inherited in a referencing assembly.

    public - no access restriction


    When defining a member in a class with no explicit access modifier, it defaults to 'private', to preserve encapsulation.

    Code (csharp):
    1.  
    2. public class Foo
    3. {
    4.     //this method is private
    5.     void Foo() { }
    6. }
    7.  
     
  4. frekiidoochmanz

    frekiidoochmanz

    Joined:
    Feb 7, 2016
    Posts:
    158
    wish I could comprehend what this is, ill look into that


    ill have to research this, thanks
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Sounds like you simply want protected...
     
  6. SlyRipper

    SlyRipper

    Joined:
    Jun 19, 2012
    Posts:
    251
    Here's an example what i mean:

    Code (csharp):
    1. class ClassA : ClassB
    2. {
    3.     public void Walk()
    4.     {
    5.         base.Walk();
    6.     }
    7. }
    8.  
    9. class ClassB : ClassC
    10. {
    11.     public void Walk()
    12.     {
    13.         base.Walk();
    14.     }
    15. }
    16.  
    17. class ClassC
    18. {
    19.     public void Walk()
    20.     {
    21.         //some Action here
    22.     }
    23. }
    Wrote this with my phone, so dont know if this also works with Private methods, at least it should work with protected.
     
  7. frekiidoochmanz

    frekiidoochmanz

    Joined:
    Feb 7, 2016
    Posts:
    158
    I feel silly now, thank you everyone, it was protected. I was trying to make a baseEntity class that all objects derive from and it needed settings methods I could access from everyone per everyone.