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

Question About Custom Inspectors And Sciptable Objects

Discussion in 'Scripting' started by yepfuk, Jan 7, 2019.

  1. yepfuk

    yepfuk

    Joined:
    Sep 23, 2015
    Posts:
    67
    Hi, I have a designing problem and couldn't solve it by myself.

    Code (CSharp):
    1. public abstract class Action
    2. {
    3.      public int actionID;
    4.      public string actionName;
    5.      public Status actionStatus;
    6.  
    7.      protected virtual void Activate()
    8.      {
    9.             actionStatus = Status.Active;
    10.      }
    11.  
    12.      protected virtual void Execute()
    13.     {
    14.           if(actionStatus == Status.Inactive)
    15.                Activate();
    16.     }
    17.  
    18.     protected virtual void Reset()
    19.     {
    20.           actionStatus = Status.Inactive;
    21.     }
    22. }
    I have action's that derived from this class, and each one have own Activate, Execute and Reset functionalities. Like this;

    Code (CSharp):
    1. public class Action_Use : Action
    2. {
    3.      protected override void Activate()
    4.      {
    5.             base.Activate();
    6.  
    7.             // Action_Use specific activations
    8.      }
    9.  
    10.      protected override void Execute()
    11.     {
    12.           base.Execute();
    13.  
    14.             // Action_Use specific executions
    15.     }
    16.  
    17.     protected override void Reset()
    18.     {
    19.           base.Reset();
    20.  
    21.           // Action_Use specific reset
    22.     }
    23. }
    Some gameobjects in my game has behaviours and this behaviours have own their action list.

    Code (CSharp):
    1. public abstract class Behaviour : ScriptableObject
    2. {
    3.      public List<Action> actionList;
    4.  
    5.      //
    6. }
    When I inherit Action from Scriptable Object I can create those actions from menu and add them into behaviours.

    But when the actions and behaviours are being used by game objects in runtime, properties of scriptable objects are change(for example if one game object changed action status to active, then the other game objects that uses same action's status also change) and this is not what I want.
    I want a solution that I can easily add this actions to the behaviours, and game objects can change this actions' properties individually.

    Can I solve this problem with scriptable objects(but different way that current implementation) or I have to use custom inspector(reordarable lists, property drawers etc.)?

    Thanks in advance.
     
    Last edited: Jan 7, 2019
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,196
    The easiest thing is to just make a copy of the ScriptableObjects at startup:

    Code (csharp):
    1. for (int i = 0; i < actionList.Length; i++)
    2.     actionList[i] = Instantiate(actionList[i]);
    This does of course have an overhead in memory footprint and startup time, but if you only have a few dozen of these, it shouldn't be a problem
     
    yepfuk likes this.
  3. yepfuk

    yepfuk

    Joined:
    Sep 23, 2015
    Posts:
    67
    Hmm, thank you for answering. I will try this today.