Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Jessy · Aug 10, 2010 at 12:53 PM · classpublic

What's the point of declaring a C# script a public class?

Public. Why bother? I never used it, and never had a problem. But Unity 3b4 introduced this issue where if you do not use it, and then you declare a public variable, you'll get a warning if you don't give the variable a default value. But I don't want to do that, because I use public variables purely so I can assign them in the Inspector! I reported this warning issue as a bug to UT, and support told me about how to solve it by using "public" in front of the class name. That's a lot cleaner than having to use "= null" or whatever, after all the public variables, but it still seems useless to me.

Comment
Add comment · Show 2
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 burnumd · Aug 10, 2010 at 02:18 PM 0
Share

It seems you've already solved the problem of getting a warning on public variables so I'm not sure what your exact question is. You might consider moving this to the forum where you'll likely get more constructive discussion.

avatar image Jessy · Aug 10, 2010 at 03:33 PM 0
Share

$$anonymous$$y exact question is "Whats the point of declaring a C# script a public class?".

2 Replies

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

Answer by jashan · Aug 10, 2010 at 04:27 PM

Actually, I'm a bit surprised that Unity does allow you to get through without the "public". When omit the "public" it means that your class is accessible "internal" which means

The type or member can be accessed by any code in the same assembly, but not from another assembly.

(Source: Access Modifiers (C# Programming Guide))

In Unity, as your scripts are accessed from the game engine runtime which is not your code assemblies (all scripts of the same language get compiled into a DLL; editor scripts and I think also plugins are compiled into separate assemblies), actually scripts which are not declared public shouldn't work, IMHO, because they are accessed from outside their assembly.

So, in general, I'd always create public classes unless I really want my classes to only be within the current assembly (which is rarely the case - would be the case for very special purpose helper-classes, though).

For a deeper understanding of encapsulation (which is what these access modifiers are all about), Wikipedia is a good starting point: Encapsulation (object-oriented programming). Another nice section about encapsulation can be found in the main Object-oriented programming article.

Comment
Add comment · Show 5 · 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 Jessy · Aug 10, 2010 at 04:53 PM 0
Share

What does "assembly" mean in the context of Unity?

avatar image Mike 3 · Aug 10, 2010 at 06:29 PM 0
Share

When you omit public it's private, not internal. Doesn't make much difference to unity though since it's done via reflection

avatar image Cyclops · Aug 10, 2010 at 06:40 PM 0
Share

@$$anonymous$$ike - "By default, a class without any access modifier is defined as internal ". From Essential C# 2.0 by $$anonymous$$ark $$anonymous$$ichaelis. Which also concurs with the $$anonymous$$SDN link that @Jashan gave.

avatar image Mike 3 · Aug 10, 2010 at 07:20 PM 0
Share

Fun! I must have been thinking of something else then. Back to holidays! :p

avatar image jashan · Aug 11, 2010 at 07:56 AM 0
Share

@Jessy: An "assembly" is a file with the extension *.dll which contains the compiled bytecode of the classes/script that you define. It's a bit tricky because people sometimes call them "DLLs" which is correct, of course - but could be misleading (a DLL is originally a "dynamic link library" which is a binary file used on Windows and specific to Windows). When working with Unity, you usually don't have to directly deal with those "assemblies" unless you include 3rd party assemblies to add APIs you're using (like log4net, for instance).

avatar image
1

Answer by Cyclops · Aug 10, 2010 at 01:39 PM

I can't say what UT's reasons are, but if you simply want to get rid of the warnings, you can (in C#) use pragmas - Disable warning messages, for instance

#pragma warning disable 0168 // variable declared but not used.
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 Jessy · Aug 10, 2010 at 01:54 PM 0
Share

How do you figure out the 4-digit numbers?

avatar image Cyclops · Aug 10, 2010 at 03:24 PM 0
Share

Read it from the Unity warning line when it happens. I think there's a prefix of some characters, ignore them.

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

No one has followed this question yet.

Related Questions

When exactly do scripts have to be named exactly the same as the class they contain? 3 Answers

Color set to black even though it's set by the editor 0 Answers

Accessing not yet publicly added scirpt 3 Answers

how to activate a public void from another script 1 Answer

CoD 4 "weapon classes" 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