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 /
avatar image
0
Question by Stavros-Dimou · Aug 03, 2017 at 12:10 PM · scripting problemeditorinterface

I want to use an interface but the editor doesn't show public properties. What should I do ?

Hello everyone. I'm making a game and I want the player to be able to pick up and carry many different kinds of items in his or her inventory. Please mind that although I have some programming experience, I've never made a game before.

So coming from a C# OOP background I thought of creating an interface "IItem" and different classes that inherit it like "Weapon", "Food" etc. I want some code to be able to work on an item no matter the type of item it is.

The problem is that interfaces doesn't take variables in their definitions but properties, and the editor does the exact opposite and doesn't show properties but variables. And I need to be able to fill in the details of each different item for each instance of it.

So, does anybody know of a workaround ?

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

3 Replies

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

Answer by · Aug 03, 2017 at 12:41 PM

Could you use Inheritance? https://unity3d.com/pt/learn/tutorials/topics/scripting/inheritance

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
0

Answer by UnityCoach · Aug 03, 2017 at 12:47 PM

Like @Nyro suggested, you can use an Abstract class. Although, you can also use `GetComponent()` or any of the GetComponent methods. You may also want to have a look at [ScriptableObject][1]. By the way, these are topics I touch on in my SOLID design course, available [here][2]. [1]: https://docs.unity3d.com/ScriptReference/ScriptableObject.html [2]: https://www.udemy.com/solid-object-oriented-programming-and-profiling-unity-5/?couponCode=MVC2SOLIDSUMMER
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
0

Answer by cgarossi · Aug 03, 2017 at 12:53 PM

Hi.

Interfaces are Abstract classes are almost the same, but different in two very distinct ways.

Interfaces are just a contract which defines what MUST be implemented by you class in order for it to be that class.

Abstract classes cannot be constructed but provide default functionality or stub methods that your class can inherit from.

For any game object you want to display in the inspector which derives from the abstract class you can just use the abstract class type in place of your specific implementation, the same way interfaces work.

Interfaces don't play nice in unity because they cannot be serialized. I would suggest creating an abstract base class that also derives from Monobehaviour or ScriptableObject plus any interface you decide to implement. Then you'll have the best of both worlds.

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

128 People are following this question.

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

Related Questions

Changing default animation type to Humanoid via script 1 Answer

How can I overwrite .asset using AssetDatabase.CreateAsset? 2 Answers

How do I solve a BuildAssetBundles Compilation Error that only occurs in the Editor? 1 Answer

Script works in Game View, but not in build 0 Answers

Can I know why I can't control volume of audiomixergroup without exposing parameters? 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