Do I understand right that its not possible to subclass Unity base classes, particularly GUITexture ? (when i have gameObject and addComponet class that inherits from GUITexture nothing happens…) (I know about writing mixing classses … long way again =)
There is no problem in subclassing in general at least none I would ever have seen
Do you have some code that shows your problem?
I assume you didn’t override any basic functionality like AddComponent or the gameObject
because if you did you must reimplement their functionality (in case of AddComponent you can just call the base AddComponent first and then do your specific stuff - but gameObject might be a bit trickier, as this is no variable but a property actually and overwritting this thing would impose even further performance costs to it than it already has)
Well, actually I did just a simple subclassing without method overriding yet…
Just for test… this is subclass…
using UnityEngine;
using System.Collections;
public class CellExt : GUITexture {
public GameObject target;
void Start () {
}
void Update () {
}
}
this is attached to empty gameObject …
public class NewBehaviourScript : MonoBehaviour {
public GameObject FirstCell;
// Use this for initialization
void Start () {
FirstCell = new GameObject ("FirstCell");
FirstCell.AddComponent("CellExt");
}
// Update is called once per frame
void Update () {
}
}
if CellExt inherits from MonoBehaviour everything work fine, you can see it in Inspector, but if it’s from GUITexture then nothing you see…
Well i didnt notice at first but there is message in a log:
Can't add script behaviour CellExt. The script needs to derive from MonoBehaviour!
UnityEngine.GameObject:AddComponent(String)
So, actually it means that you cant subclass…
it’ looks like its design bug …you can AddComponent(“GUITexture”)
but you can’t AddComponent(“GUITextureSubclass”)
You can only subclass from MonoBehaviour if you want the class to be used as a component. You can subclass from any .NET class as much as you like, but of course thats not the same as creating a component you can attach to a game object.
For all practical purposes, Unity is component based so not being able to inherit from all components is not a real world problem.
For all practical purposes, it should be that you can subclass anything…what is the real reason that you can’t use subclass ? ( using mixed classes instead of subclassing is pain sometimes…not really good design story…)
For a script of any kind to be attached to a game object it needs to inherit from MonoBehaviour.
Perhaps your should consider using an interface.
You can subclass - and you are doing it - as pointed out you inherit from MonoBehaviour.
Since C# is a single inheritance language that implies that you cannot inherit from more classes in order to be an attachable script.
That’s not true…GUITexture does not inherits from MonoBehaviour , it inherits from Behaviour/Component and you can attach it to gameObject, That really strange that you cant attach subclass of it … (also other stuff like BoxCollider has nothing to do with MonoBehaviour, it inherits from Component, and you can attach it to gameObject…)
It’s only true for my scipts…
Encountering the same problem, using AddComponent instead of new, trying to implement state override classes. Which I made into extensions, not interfaces, so that I could use GetComponent in the first place.
Please don’t respond to a 12±year-old thread. It’s against forum rules.
If you have an issue, please make a new post. It’s FREE!
How to report your problem productively in the Unity3D forums:
How to understand compiler and other errors and even fix them yourself:
If you post a code snippet, ALWAYS USE CODE TAGS:
How to use code tags: https://discussions.unity.com/t/481379
Well, it’s “pointless necroposting”. If they found this and it seemed to relate to their problem, I suppose they didn’t think it was pointless. The big obvious ones are fixing a debugging problem 5 years later.
This thread seems like a lot of misunderstanding. The OP wants to subclass from a Unity component and have Start() and Update(). Maybe they think GUITexture (which is obsolete!!) has those. But regular components don’t have that stuff. If you want to start up a TextMesh and play with it every frame, write a regular script that looks for the TextMesh component on it’s gameObject. Only subclass from TextMesh if you want a new type of TextMesh that just sits there and lets other people play with it in new ways. But you’d rarely need to do that.
Haha I didn’t didn’t realize it was from 2009. I just pick my way through the top google results. Anyway, I was just venting. but I’ll keep a closer eye out for that from now on. A blogger shared a method to build state machine classing. but it’s not compatible with Unity Component system. I won’t need to redo much, but I have to wrap my head around Mecanim for control parsing/transitions.
Just to give an actual answer to this 12+ years old thread:
There is a keyword in C# called sealed
. If a class definition uses this keyword, you cannot derive from that class (resulting in compile errors if you try).
GUITexture
and many other Unity classes use that keyword. In most cases there is a reason. However, When I reverse engineered some of these classes I couldn’t see the reason (maybe Unity is using it sometimes although it would be no problem to derive from it…).