- Home /
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:
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:
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
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?
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.
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.
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
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.
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.