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 MartinH8921 · Jan 26 at 10:43 PM · class objectsubclassing

Can I create a subclass of the GameObject class?

The reason I ask is mainly to educate myself. I currently have a set of 10 prefabs that are instantiated at runtime. I want each of these to have a set of attributes associated with them.

I know that I could attach a script to each prefab that has the attributes on it, but a subclass of GameObject might enable me to create objects in code for each prefab that would inherit the attributes from the subclass of GameObject. I'm very new to c# and Unity, so I'm sorry if this sounds totally absurd. if there is a more "elegant" solution, I'm all ears. Thanks.

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
Best Answer

Answer by dadamsj1 · Jan 26 at 11:00 PM

GameObject is a sealed class so it cannot be derived. However, you are probably looking to derive from MonoBehaviour which is the default inheritance when you make a new c# script in Unity.

You can create your class

 using UnityEngine;
 
 public class MyDerivedMonoBehaviour : MonoBehaviour
 {
     //Customize it as you want here.
 }

After you create that class you can instantiate just like a you would a gameObject prefab with the following modification:

 var myDerivedInstance = Instantiate<MyDerivedMonoBehaviour>(prefab);

Good luck!

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 MartinH8921 · Jan 26 at 11:41 PM 0
Share

If my prefabs are not instances of the GameObject class, do they lose some of the attributes of GameObject?

avatar image dadamsj1 MartinH8921 · Jan 27 at 05:02 PM 0
Share

They are still GameObjects in this case because they are derived from MonoBehaviour. It will be like a normal GameObject but with your script attached from the onset.

avatar image MartinH8921 · Jan 27 at 06:43 PM 0
Share

I'm not sure if I'm approaching this problem the right way, but I'll give you credit for giving a correct answer to my original question. Thanks.

avatar image dadamsj1 MartinH8921 · Jan 27 at 06:54 PM 0
Share

If you want to be more specific about the issue you are facing, I'd be happy to help.

avatar image MartinH8921 dadamsj1 · Jan 27 at 06:56 PM 0
Share

Basically, I'm creating a puzzle game and I'm not sure of the best way to compare the properties of the puzzle pieces with the others in order to deter$$anonymous$$e the outcome when they combine. I've been working on my own to figure it out, but I should probably follow a puzzle game tutorial and learn how "the pros" do it before tackling it on my own.

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

132 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 avatar image avatar image avatar image avatar image

Related Questions

Creating an array of custom objects 1 Answer

Adding Classes to a List of classes 1 Answer

Can't use normal class in Unity? 1 Answer

Changing inspector default values from subclass OR hide from inspector in subclass only. 2 Answers

Runtime Error IOS AOT Only 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