Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Subclassing not possible ??

Discussion in 'Scripting' started by Kulver, Feb 25, 2009.

  1. Kulver

    Kulver

    Joined:
    Jan 11, 2009
    Posts:
    20
    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 =)
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    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)
     
  3. Kulver

    Kulver

    Joined:
    Jan 11, 2009
    Posts:
    20
    Well, actually I did just a simple subclassing without method overriding yet...

    Just for test.... this is subclass...
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class CellExt : GUITexture {
    6.  
    7.     public GameObject   target;
    8.  
    9.     void Start () {
    10.     }
    11.  
    12.     void Update () {
    13.    
    14.     }
    15. }
    16.  
    this is attached to empty gameObject ...
    Code (csharp):
    1.  
    2. public class NewBehaviourScript : MonoBehaviour {
    3.  
    4.     public GameObject FirstCell;
    5.     // Use this for initialization
    6.     void Start () {
    7.        
    8.         FirstCell = new GameObject ("FirstCell");
    9.     FirstCell.AddComponent("CellExt");
    10.  
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.    
    16.     }
    17. }
    18.  
    19.  
    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:
    Code (csharp):
    1.  
    2. Can't add script behaviour CellExt. The script needs to derive from MonoBehaviour!
    3. UnityEngine.GameObject:AddComponent(String)
    4.  
    5.  
    So, actually it means that you cant subclass....

    it' looks like its design bug ...you can AddComponent("GUITexture")
    but you can't AddComponent("GUITextureSubclass")
     
  4. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    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.
     
  5. Kulver

    Kulver

    Joined:
    Jan 11, 2009
    Posts:
    20
    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...)
     
  6. rom

    rom

    Joined:
    Jul 2, 2006
    Posts:
    265
    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.
     
  7. fehaar

    fehaar

    Joined:
    Mar 1, 2007
    Posts:
    44
    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.
     
  8. Kulver

    Kulver

    Joined:
    Jan 11, 2009
    Posts:
    20
    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...
     
    mysticvalleycsa likes this.
  9. mysticvalleycsa

    mysticvalleycsa

    Joined:
    Sep 22, 2020
    Posts:
    14
    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.
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    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:

    http://plbm.com/?p=220

    How to understand compiler and other errors and even fix them yourself:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
    Bunny83 likes this.
  11. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,917
    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.
     
  12. mysticvalleycsa

    mysticvalleycsa

    Joined:
    Sep 22, 2020
    Posts:
    14
    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.
     
  13. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,076
    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...).