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 kekeoki · Oct 04, 2016 at 03:04 AM · gameobjectcomponentoopextend

Extending MonoBehaviour to share code between Objects

I am thinking in a traditional OOP way and maybe that is messing me up here, I have a game manager which has a reference to a selected object and pulls data from that object, I.E Name, array of ui options, currentLevel, goldAmount, etc

Now I have 10 buildings I use, that all have these options, and onClick my game manager uses these properties. Now If click a gameObject without these properties, it could cause problems If I don't have the correct properties on them. So is the best way to handle this using derrived classses, or to add a component to each building I.E

OOP Monobehavior->SelectableObject-> SomeBuilding (I am not sure how to accomplish this in unity)

or just add a component to the gameObject called SelectableObject that has required properties. (not 100% sure how to do this either)

Any tips or examples would be awesome! Thanks!

Sammy

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Lao2211 · Oct 04, 2016 at 05:14 AM

Last time I checked the only way to create custom components is to derive from MonoBehaviour so you are going the right way there.

SelectableObject implies that the object is selectable. Then I propose to not allow any selection at all on game objects that doesn't have that script attached.

You can check if an object has the component with something like this:

SelectableObject.cs

 using UnityEngine;
 using System.Collections;

 public class SelectableObject: MonoBehaviour
 {
     public int Health = 0;

     public static bool IsSelectable (GameObject object) {
         SelectableObject component = object.GetComponent<SelectableObject>();
         if (component != null)
             return true;
         
         return false;
 }

The other option is to just let the game fail as loud as it wishes. Let it try to access innexistent properties and do bad type casts. So you know that a scene is invalid during your tests, the end user won't see the errors because you will correct all of them prior to release.

But I think that is not very flexible, as I doubt you want an scene of all selectable objects. You probably are using some form of raycast to hit the selectable objects, but in an scene with only some objects being selectable, the rays will hit non selectable objects sometimes, we don't want a crash only for that, so check if an object has the SelectableObject component and simply do nothing if it doesn't sounds better.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Grabbing all Children and components of those children 1 Answer

Sharing components amongst different gameobjects 1 Answer

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Adding a script component through scripting 1 Answer

Getting component by name 1 Answer


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