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 KTF · Feb 06, 2012 at 12:42 AM · editorobjectcomponentinheritanceclasses

Class inheriting Runtime Classes

Hi, I'm new to unity. I'm having a problem with creating a new object and that object's variables showing up in the editor. I've created a class called Gun and inside of that class I have a constructor of course and a function called "Fire":

 class Gun{
 
 // whatever vars...
 
     function Gun( //constructor vars... ){
     
     //whatever vars = constructor vars…
 
     }
 
         function Fire(){
     
         // Fire's gun 
     
         }

Then I declare and create new objects from the Gun class, say…

 var ak47 = new Gun( // Gun vars);

So in the editor it'll show the variables in that new gun:

alt text

Ok, everything's looking good up to this point right?… Now here's where the problem comes in.

My "Fire" function requires the Component class because it uses Transform, audio, and Instantiate. So to fix this I extended the Component class to my Gun class:

 class Gun extends Component{

Now when I do this it fixes all the errors and makes the function "Fire" run as intended but now when I declare and create a new gun object, lets say the AK-47 again:

 var ak47 = new Gun( // Gun vars);

Now all the variables for the newly created gun object are gone and I'm left with this:

alt text

I know it has to do with the Gun class inheriting the Component class of course but I have no idea about anything after that.

I'm still really new to unity and moderately new to programming. Since this is a free service I highly appreciate any and all input and answers.

Thanks, Kev

Comment
Add comment · Show 5
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 tingham · Feb 06, 2012 at 02:23 AM 0
Share

I think you'd want your class to be based on monobehaviour in any case and at some level. From what it looks like here you've got one giant class with a bunch of subclasses enumerated on it as properties.

Have you done the tutorial project that unity provides yet?

avatar image KTF · Feb 06, 2012 at 02:37 AM 0
Share

Ive tried extending it to $$anonymous$$onoBehaviour but $$anonymous$$onoBehaviour doesn't contain Transform, audio, and Instantiate, which my functions requires in the class. $$anonymous$$onoBehaviour doesn't support the keyword 'new' as well so I can't use it.

avatar image tingham · Feb 06, 2012 at 02:42 AM 1
Share

The general idea is that you attach your script to a GameObject which includes all of the aforementioned items. $$anonymous$$onobehavior is then just the "behavior" of an object in your game.

When you get into doing any substantial depth of class inheritance in Unity (like what you're trying to do) you really are just making the problem more difficult for yourself.

In most of the stuff we're building now, we will derive from one base class that extends monobehaviour and then use collections to manage instances of classes; but in almost all cases we're correlating on a 1:1 basis with some game object in the scene.

The reason I asked about the plat former tutorial is because they really hammer home the idea that these classes are more like "delegates" than actual objects in your scene.

avatar image KTF · Feb 06, 2012 at 02:57 AM 0
Share

I think I might get it. I'll take a look it and post if i've figured it out or have more Q's later. Thanks

avatar image KTF · Feb 06, 2012 at 03:15 AM 0
Share

Well i feel pretty stupid. You were completely right I was making things way more complicated then they needed to be. I just simply moved the function outside of the class and simply accessed the variables inside of the class from within the function Thanks! You should put your answer into answer form so i can give you credit.

1 Reply

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

Answer by tingham · Feb 06, 2012 at 03:20 AM

Long story short: You want to extend mono behaviour instead of trying to subvert the GameObject/Component structure that Unity uses for everything. Think of your classes as "things to attach to game objects" instead of "things that are game objects" and you'll have a lot less trouble.

It's also important that you're not trying to over-design your solution but that has more to do with general programming experience than anything else.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Composition: Multiple actions on death 1 Answer

Script component in Editor vs. runtime-dependent inheritance 0 Answers

How unity works under the hood? 0 Answers

Inheritance and Component Types in C# 2 Answers

Custom "Can't add due to conflicting component" 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