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
1
Question by jwulf · Feb 22, 2017 at 01:15 PM · c#componentspublicarchitectureprotected

Restrict access to components on same object

I've caught myself several times now defining Properties/Methods as protected when they actually should be public. I made this mistake be cause I wanted to achieve the following: Define Properties/Methods to which only other Components on the same Object have access.

A use case for example would be: I have an enemy with an EnemyController-Component and multiple EnemyState-Components, where the Controller should be able to acceess the State's properties (e.g. there could be a Patrol-State and the Controller should be able to set patrol-waypoints) but no other gameObjects should be able to do so.

But is there a way (probably not via access modifiers, but some other) do achieve what I described?

I know of course, that (staying in my controller and patrol state example from above)

  1. there would be other ways to go, for example keeping the patrol-waypoints in the controller and make them publicly gettable, so the patrol-state could access them without "owning" them

  2. I (or the developer who uses my objects) would still have to ensure that only "authorized" components of the same object alter the patrol-state.

But by restricting access to components of the same object I prevent some possible mistakes (altering an enemy state from "outside", which should in my example never happen), which is imho always good.

So is there a reasonable way to achieve this access restriction? Or would it be, for some reason I don't see at the moment, useless anyway?

Thanks for any input! I hope, this is not an irrelevent/to generic question.

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

1 Reply

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

Answer by Forlinim91 · Feb 22, 2017 at 02:40 PM

There's no "component visibility" in unity, so no, you can't prevent the GetComponent commands family from getting whatever component is attached to a gameobject.

Of course, unless you create your own MyGetComponent command which explicitly filter out those components (return null) and eventually call the original GetComponent command, like this:

public static T MyGetComponent <T> (this GameObject go) where T : Component {
    if (typeof (T) == typeof (ComponentWhichMustNotBeAccessed))
        return null;
    else
        return go.GetComponent <T> ();
}

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 jwulf · Feb 22, 2017 at 03:00 PM 0
Share

I see what you mean; I could kind of introduce GetComponentFromOutside built as your example and use that from any "outside"-GetComponent-Calls and try to maintainthe rule that any GetComponent-Calls co$$anonymous$$g from other GameObjects use that ins$$anonymous$$d of GetComponen; so for any developer (including myself) there is some sort of "re$$anonymous$$der" what is to be accessed and what not.

I'll think about it ;) thank you for your answer and your suggestion.

BTW: You wrote

There's no "component visibility" in unity

Is there in some other Frameworks using C#? Since components, from my understanding (never used c# out of unity), are generally a part of C#, right?

avatar image Forlinim91 jwulf · Feb 22, 2017 at 04:08 PM 0
Share

Nope, components are introduced by Unity and don't depends on the program$$anonymous$$g language. I only meant there's no support for "visibility restriction" in a component with the GetComponent command, but you have to create a pseudo-visibility yourself, like in my example.

From the program$$anonymous$$g side (C# and Javascript) a component is only a subclass of the type Component. When you create a script, usually it's a $$anonymous$$onoBehaviour (which is Behaviour, which is a Component).

In other words, a component means nothing to C#. It's just a class like the others. When you attach a Component to a GameObject, you're only creating an instance of a class which extends Component, which is passed to the GameObject, which store it in its internal data.

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

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

Related Questions

Using interfaces in C# 4 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Setting public GameObject to a different Prefab through code 0 Answers

What´s the cleanest way to link physics-events and gameobjects? 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