Help with class inheritance Vs. scripted components

Hello,

I’ve been studying Unity for a while now, and I’m familiar with OOP, have made little games with my own Classes in other languages, etc. I’m still having trouble figuring out the best approach for this situation, and Google searches for examples or tutorials have been unsuccessful (most tutorials are really basic, or focus on the editor side, instead of the scripting side).

So here’s my scenario:

Every time the Player uses an item that is already equipped, I want to call something like a “gameobject.Item.Use()” method. The problem is that I want to have different kinds of items, like Keys or Guns, but I want them all to inherit from a base 'Item" Class, like this:

  • Class Item
  • Class ItemKey Inherits Item
  • Class ItemGun Inherits Item

Creating the classes is easy, but how do I go about “adding” these to an existing gameobject? Can I create a script that extends another script, since all scripts are classes that extend MonoBehavior (as far as I understand)? If yes, how? Sorry if the question sounds basic, I understand most of the concepts involved, I’m just having trouble putting it all together.

I’d love to see links for tutorials or examples of different ways to organize / structure scripting in a real world scenario.

It may be worth mentioning that I DON"T want to create everything in scripting, since that kinda defeats the point of using Unity for me, I want to have a blend of creating and managing objects in the editor, and just assigning scripts as i go.

Thanks! :smile:
Leo.

P.S. I’m using UnityScript.

I think you can still use base classes, by like someclass extends baseclass, in C# (what i use) i do this

public class BaseClass : MonoBehaviour {

}

public class InheritedClass : BaseClass {

}

it works for me :slight_smile:

Yep, EliteMossy’s method is what I also do. In UnityScript you might need to use the keyword “extends”, I’m not sure as I’m a C# guy myself.

Cool, I get it now!

I can define a class with the same name as the script, right?
Let’s say I have a “Item.js” script. That is, in itself, an “Item” class. Then I can do this:

in script file “ItemGun.js”

class ItemGun extends Item {
	function Use() {
		print( "Pew!" );
	}
}

I didn’t know you could do that! Define a class that has the same name as the script, inside the script. Then it works like I want, right? The new component will inherit everything from the basic Item component.

Let me know if there’s anything essentially wrong about what I just said!
Thanks!

Yep. In fact, not only you can name it the same as the script, that’s how it’s generally expected to be done and even, in some languages, what’s required.

The new component does inherit everything, but it can’t necessarily access everything. It can only access what is exposed, which means anything marked as ‘public’ or ‘protected’. Stuff marked ‘private’ can only be accessed by the class it’s a member of.

ooo, been wondering how unityscript does classes… thanks for this :slight_smile: