Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Garrom · May 02, 2017 at 05:24 PM · variableinheritancehierarchyscripingprotected

Is protected variables protected in hierarchy ?

Hi all. I wonder if is protected variables in some class on gameobject available only in classes inherited from parent class or if is available in non - inherited classes in child gameobjects. Thanks.

-Garrom

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
1

Answer by Bunny83 · May 02, 2017 at 06:40 PM

Any class member with the access modifier protected behaves like a private member from outside the class. The only difference to private members is, that derived classes can access them.

However note that private and protected apply to the "domain" of a class and not necessarily to one specific instance.

For example:

 public class Test
 {
     private int privateInt;
     protected int protectedInt;
     public Test(int aValue)
     {
         privateInt = aValue;
         protectedInt = aValue;
     }
     public bool HasSameValue(Test aOther)
     {
         return aOther.privateInt == privateInt;
     }
 }
 
 public class Test2 : Test
 {
     public Test2(int aValue) : base(aValue) { }
     public void Print()
     {
         Debug.Log("protectedValue: " + protectedInt);
     }
     public void SetProtected(int aValue)
     {
         protectedInt = aValue;
     }
 }

As you can see inside the "HasSameValue" method of the Test class we access the private variable of another Test instance. This is only possible because this code is inside the Test class.

The derived class Test2 can't see or access "privateInt" but it's still there. Methods or properties of the Test class might indirectly access the private variable. However the Test2 class as well as any other class can't access it directly.

protectedInt can be accessed by Test2 since Test2 is derived from Test and therefore it sees all protected members. Though when a Test2 or Test instance is used in a class that is not derived from Test, "protectedInt" won't be accessible.

 Test2 obj1 = new Test2(42);
 Test obj2 = new Test(42);
 
 
 //Debug.Log("value: " + obj1.protectedInt); // this line WOULDN'T work

 if (obj1.HasSameValue(obj2))  // this does work
     Debug.Log("same!");

HasSameValue will compare the "privateInt" variables of those two instances, even though it can't be accessed from outside. The Test2 instance doesn't even has access to it's own privateInt but the method defined in Test can access it.

Comment
Add comment · Show 2 · 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 Garrom · May 02, 2017 at 07:11 PM 0
Share

Well... you explained me Inheritance betwen classes but i asked if protected variables will be visible from non-inherited class what is on child gameobject( so it inherit by hierarchy but not by code). So i asked if protected variables on Script1.cs will be visible in Script2.cs what isn't Inherit directly by script but is child in hierarchy so is Inheritance in hierarchy connected to hierarchy in-code ?

avatar image Glurth · May 02, 2017 at 07:29 PM 1
Share

@Garrom Class system inheritance hierarchy in C#, and the object hierarchy in the scene are completely separate structures. The ONLY similarity is that the words used to the describe the structures are the same/similar. Edit: The first is defined by the "c# standard language definition", and applies to any c# program, including non-Unity ones. The other (object scene hierarchy) exists only as part of Unity, and is not part of the c# language (though unity HAS created a library of c# classes [UnityEngine] to access it).

avatar image
0

Answer by FM-Productions · May 04, 2017 at 01:59 PM

Hi,

Protected variables are only accessable and visible in the class in which they are declared and in all classes that inherit from that class. The access modifiers to use so that variables are also visible in non-inherited classes are "internal" and "public". Do not confuse the concepts of access modifiers in C# with the Object Hierachy in Unity. I think all of your gameObjects scripts will inherit from MonoBehavior and they do not inherit from each other? Then protected variables are not visible in other gameObject scripts.

For more about access modifiers, you may have a look here: http://www.c-sharpcorner.com/uploadfile/puranindia/what-are-access-modifiers-in-C-Sharp/

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

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

69 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to check if variable exists? 1 Answer

HideInInspector with inherited variables 1 Answer

GameObject Instantiate and check if "activeInHierarchy" (HELP) 0 Answers

Parent Class variables in Child Class's Inspector (C#) 0 Answers

Using a script as a member variable in another script. 2 Answers


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