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 SassMaster · Sep 24, 2014 at 05:24 PM · interface

Should I do all interfaces in one script?

I've been wondering about what I should use for interfaces, separate interfaces for each script? Or would it be wiser to put all interfaces in one script and implement them from there?

Comment
Add comment · Show 7
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 nooneknows · Sep 24, 2014 at 05:55 PM 0
Share

I rather separate the scripts, because of the order of compilation.

avatar image SassMaster · Sep 24, 2014 at 06:29 PM 0
Share

I thought it'd be more of a preloading system so that I could keep all of the interfaces somewhere accessible, and load all of them at the beginning of the game.

avatar image SassMaster · Sep 24, 2014 at 06:45 PM 0
Share

In the meantime, I'm still confused on how to actually use an interface. I know how to make an interface, but whenever I try to actually implement it into another script, I get a compiler error saying: error CS0246: The type or namespace name `canTakeHostage' could not be found. Are you missing a using directive or an assembly reference?

avatar image Habitablaba · Sep 24, 2014 at 06:55 PM 2
Share

An interface is simply a contract that says 'Anything that implements this interface will implement the following methods...'
Each interface should be in its own file, and each implementation of that interface should be in its own file as well. What does your interface look like?
What does your implementation look like?

avatar image dmg0600 · Sep 24, 2014 at 07:20 PM 0
Share

Don't use scripts on other namespaces. Unity works only with the main namespace, that is why you don't see any namespace declaration in Unity scripts.

If you do use namespaces for your non-Unity classes and interfaces, be sure that you add the using clause for that namespace: using yourNameSpaceName;

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Habitablaba · Sep 24, 2014 at 07:58 PM

Based on the code you posted in your comments, you've actually defined an interface within another class.
Code works with the idea of 'scope,' or levels of access. What you've done in made it so the TileInterfaces class is the only one that knows about the canPlantCharge interface.
canPlantCharge should be the top level scope in the file, meaning the entirety of the canPlantCharge.cs file should be

 public interface canPlantCharge
 {
   void PlantCharge();
 }

Alternatively, you could make your WallFunctions say:
public class WallFunctions: MonoBehaviour, TileInterfaces.canPlantCharge {
But that is all manner of strange, in my opinion.

More on scope:

 public class ScopeExample: MonoBehavior 
 {
   // Everything declared here is at 'class scope'  
   // which means they can interact with each other. 
   // This includes fields, methods, and properties.  
   // Using the 'public' keyword allows other entities
   // outside of this class to access certain class scope items
   public int a; // accessible from outside the script.

   public void Method1()
   {
     // Everything declared in here is at 'method' or 'local' scope.
     // Things here have access to other items defined in this method,
     // as well as all things defined at class scope level
     a = 10; // valid
     int b;

     if (true)
     {
       // variables declared here will 'fall out of scope' 
       // at the end of this if block. This means you can use
       // them between these curly braces, but not after.
       // You still have access to local and class scope variables here!
       int c;
       a = 11; // valid
       b = 12; // valid
       c = 13; // valid
     }

     a = 14; // still valid
     b = 15; // still valid
     c = 16; // NOT valid!
   }
 
   public void Method2()
   {
     // Even though we are at local scope again, we are in a different 
     // 'local' so things which are declared in Method1 are not accessible here!
     int d;
     a = 17; // still valid
     b = 18; // NOT valid
     d = 19; // valid
   }
 }


















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 SassMaster · Sep 27, 2014 at 06:40 PM 0
Share

I already know exactly how scope and access modifiers work. I'm not a complete beginner.

avatar image Habitablaba · Sep 30, 2014 at 11:46 PM 1
Share

That's great! You still messed up your scope there, though.

avatar image Bunny83 · Oct 01, 2014 at 01:38 AM 1
Share

Exactly :) If you declare "canPlantCharge" inside the class "TileInterfaces", when implementing that interface you would have to use

     public class WallFunctions : $$anonymous$$onoBehaviour, TileInterfaces.canPlantCharge
     {
avatar image
0

Answer by Kiwasi · Sep 24, 2014 at 07:01 PM

Its really up to you. One thing to consider is reusability of the interfaces/script file in other projects.

Unity will not care either way.

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 Habitablaba · Sep 24, 2014 at 07:10 PM 2
Share

While I agree that technically you can define as many interfaces, classes, structs etc. as you want in any file, I strongly disagree that you should do so.

avatar image Kiwasi · Sep 25, 2014 at 12:00 AM 0
Share

@Habitablaba you are correct. Hence why I called out reusability as a concern. Two unrelated classes/interfaces/structs have no business being in the same file.

On the other hand there is some benefit from including related classes in the same file. Say the IDamagable and the IDealsDamage interfaces.

When the interfaces and classes get complex you its also worth considering separate files inside a namespace.

When it comes to the final product however Unity does not care. The entire script files structure is compiled away. So there are no real efficiency gains to be had.

avatar image Bunny83 · Oct 01, 2014 at 01:44 AM 0
Share

Another thing is: version control! When you have your project in a git / svn / ... repository it's always a good idea to have every class / interface / enum / ... inside it's own file, named like contained type. That way you see in the version history when you changed something on a particular enum / interface.

Sometimes it looks / feels a bit cumbersome, but for large projects it's a must. To organise elements you usually use namespaces and folders.

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

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How do I get gameobjects to fire a method based on an interface? 1 Answer

Spreadsheets and Decision Tables 2 Answers

Is there a system for automating the interface, like Maxscript? 2 Answers

Loading interface 0 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