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
0
Question by robinking · Mar 03, 2011 at 10:22 AM · classnamescriptdoesntmatchclass

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

Am I correct in thinking it is only when a C# script contains a static class?

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 Statement · Mar 03, 2011 at 12:49 PM 0
Share

No, that's not correct. Eric5h5 provides a good answer. It's for all classes that derive from $$anonymous$$onoBehavior (and possibly ScriptableObject and Editor classes).

3 Replies

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

Answer by Eric5h5 · Mar 03, 2011 at 10:28 AM

It's when you want to attach a script to some object. Static classes or not makes no difference.

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 robinking · Mar 03, 2011 at 10:35 AM 0
Share

Is that only in C#? I've been attaching differently named JS scripts to objects with no ill effects... And for instance, what if you want to attach 2 scripts to the same object?

avatar image Statement · Mar 03, 2011 at 12:48 PM 0
Share

Eric5h5 means that any scripts (classes derived from $$anonymous$$onoBehavior) need to be placed in a file with the same name as the class. You can add other classes to the file, given the first class is the one named after the file IIRC. If you write plain classes you don't need to name them the same, although it's good practice so you can navigate your source code more easily.

avatar image robinking · Mar 03, 2011 at 03:21 PM 0
Share

Thank you all, that has cleared it up for me greatly!

avatar image Eric5h5 · Mar 03, 2011 at 07:39 PM 0
Share

@Robin $$anonymous$$ing: In JS, a script is automatically a class with the same as the script name, so it's not an issue in the first place. With C#, you have to explicitly put a class in the script.

avatar image robinking · Mar 04, 2011 at 01:02 PM 0
Share

Ah, I see. Thanks... I'm starting to get my head round this now.

avatar image
2

Answer by Demigiant · Mar 03, 2011 at 10:27 AM

Uhm, with C#, scripts should always have the class name equal to the filename (if that's what you mean), at least if you want to use them. Sorry if I misunderstood: this question puzzles me :P

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 Eric5h5 · Mar 03, 2011 at 10:31 AM 0
Share

No, you can use C# scripts which have a different class name compared to the file name, as long as you don't try to attach them to anything. (So they can't be $$anonymous$$onobehaviour scripts obviously.)

avatar image Demigiant · Mar 03, 2011 at 12:00 PM 0
Share

Interesting: didn't know about that :) Anyway, I wonder why somebody would want class names to be different from filenames: doesn't it get terribly confusing? Or is there some scenery in which it's useful?

avatar image Statement · Mar 03, 2011 at 12:46 PM 2
Share

There are instances where you want to go with partial classes, or nested classes which fall short of this category. Also, some people might want to bunch up a few helper classes close to the main class that use them. However, this enforcement of classname-to-filename in C# is something unique to Unity for me. I haven't seen this enforced in other areas regarding C#. I know Java follow this convention, but that's about it. I assume it has to do with compiling individual scripts, possibly something related to JS or Boo.

avatar image
2

Answer by Parthon · Mar 03, 2011 at 01:19 PM

It's only C# objects that derive from MonoBehaviour that you want to attach to another object.

This is like 90% of the use of scripts anyways, so it's a good idea to name files after classes.

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

No one has followed this question yet.

Related Questions

The scripts file name does not match the name of the class defined in the script! 1 Answer

Is a clone object still a (clone) if you change the name of it using script? 1 Answer

Accessing another script from a class 3 Answers

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

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