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 sparkzbarca · Apr 26, 2013 at 11:16 PM · programming

Can you have an abstract property/variable in a class?

So basically what I want is to know the best way or easy way to ideally get the compiler to throw an error if a variable I assign to an abstract class goes unassigned in value.

For example

if I have an abstract weapon class, pretty much all weapons will have a rate of fire and a Fire function.

Now i can declare a Fire function and if i don't do something with that function in a class that inherits I get tossed an error. If I have the variable RateOfFire though and never assign it no error is thrown.

This becomes a problem because like if i have a projectile base class and it has a damage value. I want to set a damage value for each of the "bullet" types in my game. However if i forget it could take quit a while to figure out why i'm getting a damage of 0 or 1 or whatever the default is or even the last weapons damage or whatever.

Basically even if perhaps i have a stun weapon that does zero damage i'd like to specify it does zero damage because i'd rather it toss an error and type damage = 0; than end up forgetting to make my rockets do damage and go WTF why isn't anything dying or its sometimes dying after I shoot and maybe sometimes it 1 hit kills cause it has the damage of my nuke weapon i just used or something.

Should i use a if statement or something or what is there a way to force a class variable to be specifically assigned a variable by an inheriting function?

Comment
Add comment · Show 1
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 sparkzbarca · Apr 27, 2013 at 12:59 AM 0
Share

any help? does the question make sense?

I just can't imagine no one has ever wanted to force a variable to be abstract before

1 Reply

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

Answer by whydoidoit · Apr 27, 2013 at 02:26 AM

You could make it an abstract property but that won't help that much as it won't show up in the Unity inspector.

It sounds to me like you should define a base class abstract method called verify object that calls a method that will be overridden in each subclass and whose job is to verify the settings that you have made. I'd just make that a series of Debug.LogError if in the derived class something hasn't been set.

Comment
Add comment · Show 6 · 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 whydoidoit · Apr 27, 2013 at 02:36 AM 0
Share

And actually an abstract property would just make you define how it worked, not set a value.

avatar image sparkzbarca · Apr 27, 2013 at 02:39 AM 0
Share

Thanks! I'll probably just go with the property. I don't $$anonymous$$d it not showing up in the inspector. I don't know i just never change values there, I don't know why but i don't care to use it for that.

I use the editor values mostly just to debug quickly by pause check and to manually move objects to a position (placing a camera for example at why to my eye appears to be the correct place for a 3rd person camera to sit at)

I don't really use it ever to adjust variables on the fly.

perhaps because I use C# so unless i declare it public it isn't viewable by default anyways.

I was considering the property I was just wary (i'm still a new programmer and still unsure of good coding practices, I have read about about don't have empty classes really or empty functions and stuff and I was worried that having a property which is basically a function nearly, just for the sake of making a variable abstract was bad coding practice)

avatar image sparkzbarca · Apr 27, 2013 at 02:43 AM 0
Share

well i was basically looking at this

http://www.codinghorror.com/blog/2006/08/properties-vs-public-variables.html

the thing is the code at the top is what I was going to do and it basically says that I shoudlnt do that :P

avatar image whydoidoit · Apr 27, 2013 at 02:48 AM 0
Share

Well it says you shouldn't then says you should :D

If you're program$$anonymous$$g .NET in Visual Studio for any modern framework then pretty much everything should be a property (they even have a short syntax for it now), however as Unity doesn't support properties in the inspector (hopefully one day it will grow up and do that!) it's not much point using them unless you need it at runtime.

They also would only force you to define the handling of the property, not the value.

This is a classic example of where unit tests would be useful. Perhaps you should consider that? Having a test scene that instantiates objects and tests that they are right - then you could have an editor script that opened that scene and logged it.

Basically every time you write an object you write a test class for it that is only compiled if UNITY_EDITOR is defined.

avatar image sparkzbarca · Apr 27, 2013 at 02:53 AM 0
Share

I will look up unit tests and how they are done.

I'm self taught which isn't a big deal expect that it means I can end up program$$anonymous$$g for a year and if i've never run into a problem and been told oh there is this thing called unit tests or something. Basically until I run into problems where the solution is "this" I don't learn it.

I mean I read a program$$anonymous$$g book to start but yea. I didn't know what abstract and virtual classes were until I was like well i know I want to use inheritance (which my book covered and i knew about from reading on classes) and got to reading about it more and was like well I want every weapon to have a fire function and i want inherited functions to be FORCED to define it. How can i do that?

So off to read about unit tests!

Thanks as always for your help

Show more comments

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

13 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

Related Questions

Multiple Cars not working 1 Answer

Problem with 3D platform tutorial 2 Answers

Can be mesh asset saved with an editable parameter? 0 Answers

Best Language for Unity 0 Answers

Problem with getting a value from a enum 2 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