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

Extending a class and copying values to the new sub-classes component

Discussion in 'Editor & General Support' started by Black_Stormy, Apr 21, 2019.

  1. Black_Stormy

    Black_Stormy

    Joined:
    Jan 20, 2013
    Posts:
    3
    I have `GameMode : MonoBehaviour` which has a bunch of game-related variables. For instance

    Code (CSharp):
    1. public class GameMode : MonoBehaviour
    2. {
    3.     public int score;
    4.     public Player player;
    5. }
    6.  
    I am assigning the `Player` via dragging and dropping in the editor. (This is way simplified, I have heaps of editor-assigned variables on this thing in my build).

    Now I extend my `GameMode`:

    Code (CSharp):
    1. public class NewGameMode : GameMode
    2. {
    3.     // inherits score and player from GameMode
    4. }
    5.  
    When I go to assign my GameMode script to my gameobject in the editor, all the fields are (uderstandably) blank, and I need to re-assign my `player` object, and all my other game mode variables. It's a time consuming and error-prone process.

    I can right click on the parent class component in the editor and click `copy component` but then the `paste component values` option is greyed out when I right click on the newly added subclass component in the editor. It would be cool, usability-wise, if the subclass could accept any unmodified, inherited variables from it's parent class, and vice versa. Or is this already possible?
     
    lavos2300, samochreno and cmann like this.
  2. cmann

    cmann

    Joined:
    Aug 1, 2015
    Posts:
    30
    Yes. I've also needed to do this a few times now.
     
    samochreno likes this.
  3. cmann

    cmann

    Joined:
    Aug 1, 2015
    Posts:
    30
    Actually found this write after replying here:
    http://answers.unity.com/answers/1537964/view.html

    Basically you create/copy the base script first. Then change the inspector to debug mode, which allows you to edit the script field and select the child class.
     
  4. Deleted User

    Deleted User

    Guest

    Second that. Same goes for prefabs if you want to override a class in a variant. Add the class a second time, change it to the derivate through inspector debug mode and remove the existing one
     
  5. maltakereuz

    maltakereuz

    Joined:
    Mar 29, 2015
    Posts:
    52
    >change it to the derivate through inspector debug mode
    Wow, that's just great! Thank you! Very unobivious thing. I am making enemies and have copied all basic properties 10+ times, until I have googled this.
     
  6. justHIM

    justHIM

    Joined:
    Feb 2, 2020
    Posts:
    1
    Does this still work in Unity 2022+? I updated to the latest patch, only to find that this no longer works. https://postimg.cc/HJpCkWJ0 (In this you can see the script component is now uneditable)
     
  7. Infinite-3D

    Infinite-3D

    Joined:
    Jan 5, 2020
    Posts:
    37
    It works for me in Unity 2022.3.2f1. Which Unity 2022 version are you using?