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 /
  • Help Room /
avatar image
0
Question by Joppix · Sep 21, 2015 at 07:53 PM · scriptingbasics

Why Shouldn't i put every function Public?

I don't know if i understood well, but as i kwon a private function can be called just from the class where it is, And a public function can be called from anywhere. Now: Why shouldn't i put everything on public? What should i put on private? I mean, if i put something on private or on public, what does change?

Sorry for really bad english, hope in an answer!

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
1
Best Answer

Answer by Dave-Carlile · Sep 21, 2015 at 08:53 PM

Private methods and fields are intended for things that are used internally by a class. The only things that should be public are things that need to be visible outside of the class in order for that class to be useful.

For example, I have Class A that creates a texture for me by doing some complex operations. Nobody outside of the class cares about those operations or the fact that an array and some other fields are used to cache things to make them more efficient. In fact, if that cache was public, Class B could change the cache and break Class A because it could ruin the assumptions that Class A makes about the validity of the cache. So that cache is safely hidden inside Class A where only Class A can change it.

In other words, access modifiers (that's what public, private, internal, etc. are called) are there to allow you to properly organize your code to help prevent bugs. As a rule, if something doesn't need to be public, don't make it public.

If you're just learning to write code I wouldn't worry about it too much. Learn the basics first and worry about "proper" object oriented programming when you start writing a lot more code.

Comment
Add comment · Show 1 · 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 Joppix · Sep 22, 2015 at 02:24 PM 0
Share

Ok, thanks, i think i'v understood :). For now, if i don't nedd to do something complex, i'll put my functions as private, and if i have any dubt i'll post it here :). Thanks for the answer!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Google play services? 0 Answers

need script for launching missile 0 Answers

Acceleration issue 0 Answers

Unity Scripting Learn 1 Answer

C# find game object with tag and set active? 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