Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Resolved Class Inheritance

Discussion in 'Getting Started' started by Involve_X, Oct 31, 2022.

  1. Involve_X

    Involve_X

    Joined:
    Sep 2, 2022
    Posts:
    3
    Hi, I am new to Unity and I have been writing scripts that inherit each other, but in a way that I think is bad, and I was wondering if I could do it in another way. Project VX - Microsoft Visual Studio 10_31_2022 8_06_47 PM.png

    Over here, I need the PlayerControl Class to inherit SpeedBoost, but I also need MonoBehaviour as well
    in both of the classes, and since I can't have it in both of the classes, I have to create a loop.
    Project VX - Microsoft Visual Studio 10_31_2022 8_06_53 PM.png

    And here, I need the DisplayRPM class to only be inherited to PlayerControl, but I have to inherit a few other classes to lastly get to the MonoBehaviour class.
    Project VX - Microsoft Visual Studio 10_31_2022 8_06_58 PM.png

    Here as well, I only need the GenerateObstacles class to be inherited to PlayerControl
    Project VX - Microsoft Visual Studio 10_31_2022 8_07_03 PM.png

    And it goes on into a loop, until I finally get to the class that inherits MonoBehaviour, the one I need in all of the classes.
    Project VX - Microsoft Visual Studio 10_31_2022 8_07_08 PM.png

    Now, the question is: Is there any way I could inherit MonoBehaviour in all of the classes, without having to inherit each class into the other one, just to finally get to MonoBehaviour?

    So, to wrap things up: I need for the classes to be inherited to PlayerControl only, but not between each other, but at the same time I need to use MonoBehaviour in all of them, which gives me the error.
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Yeah, that's a mess.

    You keep claiming "I need X to be inherited to Y," but frankly I don't believe you. I think you're understanding what inheritance (in programming) means, and what it is for.

    So, please back up. What is it you're trying to accomplish?
     
  3. Involve_X

    Involve_X

    Joined:
    Sep 2, 2022
    Posts:
    3
    So, I need a few of the public variables from the other classes to be inherited to the PlayerControl class. But, i also need MonoBehaviour as well in all of the classes, and if I try to inherit the classes into the PlayerControl class, I get an error saying I am inheriting MonoBehaviour multiple times.
     
  4. Involve_X

    Involve_X

    Joined:
    Sep 2, 2022
    Posts:
    3
    Update: I figured out what I should have done, and it was declaring variables as static and access them trough: ClassName.Variable