Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
4
Question by sparpo · Aug 18, 2016 at 09:36 PM · c#variablepublicprivatedifference

public or private, but what about no class before a variable?

I know this is probably a common question. Can someone explain to me the difference between public and private? Then what does having none of those before a variable? Other answers say its one or the other but i can write variables without them. I know that with public variables I can see them in inspector.

 int i = 1; 
 
 private int a = 2;
 
 public int o = 3; //whats the difference?
Comment
Add comment · Show 1
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 Owen-Reynolds · Aug 19, 2016 at 06:15 AM 2
Share

The trick is that C# is a real computer language. So looking this up with just regular "C# public private" will give much better explanations that anything from a Unity site.

3 Replies

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

Answer by Jessespike · Aug 18, 2016 at 09:49 PM

Those are Access Modifiers.

If none is specified, then by default it's private. Basically, public variables can be access by other classes and code. private variables can only be accessed within the class they are declared in. Public variables are also accessible from the Inspector.

You should really watch the Unity tutorials or something, google for C# basics, etc.

  • https://unity3d.com/learn/tutorials/topics/scripting/scope-and-access-modifiers?playlist=17117

  • https://msdn.microsoft.com/en-us/library/ba0a1yw2.aspx

  • https://msdn.microsoft.com/en-CA/library/ms173121.aspx

Comment
Add comment · Show 3 · 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 sparpo · Aug 22, 2016 at 08:18 PM 1
Share

So is there any point to putting "private" in front of a variable when by default it is private? I see some people specify "private" before a variable, but that's useless, right?

Also is there any way to make a public variable read only? So that it's only changeable in script but you can just view it from the inspector.

avatar image Graphics_Dev sparpo · Aug 22, 2016 at 08:43 PM 0
Share

It makes it clear to anyone who wouldn't know immediately that it defaults to private...and just say what if it changed to defaulting to public :(

So the extra typing saves time in the long run b/c you will easily see that its private.

avatar image Owen-Reynolds sparpo · Aug 22, 2016 at 08:52 PM 1
Share

See my comment under the Q. Even in this answer, notice how it cites standard C# (microsoft) references.) StackExchange has discussion about this (which is really all just opinion, but they explain why.)

avatar image
1

Answer by Grendl · Aug 18, 2016 at 10:57 PM

You can't use 'private' or 'public' outside of a class. Declaring a variable as private will make it visible only within the scope of the class. So within methods and constructors inside the { and } of the class definition. Declaring a variable as public will make it visible outside the class as well. The same applies for methods defined within a class.

 public class A {
    private int number1;
    public int number2;
 }

A method outside of this class will only have access to variable 'number2'.

 void someMethod() {
    A objectA = new A();
    A.number2 = 5; //This will work
    A.number1 = 0; //But this will not work
 }
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
avatar image
1

Answer by Mukabr · Aug 18, 2016 at 09:49 PM

They are access modifiers and help us implement Encapsulation (or information hiding). They tell the compiler which other classes should have access to the field or method being defined.

private - Only the current class will have access to the field or method.

protected - Only the current class and subclasses (and sometimes also same-package classes) of this class will have access to the field or method.

public - Any class can refer to the field or call the method.

This assumes these keywords are used as part of a field or method declaration within a class definition.

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 khushboo1999 · Jun 27, 2018 at 07:43 AM 0
Share

Hey, but I have seen while writing c# scripts that if I add private in front of a void it shows error and if I remove it,it works completely fine. So any explanation for that?

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to give Vector3 position to object being instantiated 1 Answer

Access to public variable of instantinated prefab not working 1 Answer

cant access variable due to its protection 1 Answer

How do I compare if a variable of integer type is between two values 1 Answer

public or private 4 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