Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Creating random events as scriptable objects

Discussion in 'Scripting' started by Jertzu, Apr 27, 2024.

  1. Jertzu


    Jan 6, 2017
    Hello all,

    I'm creating game prototype and I'm trying to create a random event system.
    When a random event is triggered, the event panel will update with the event text and choice buttons.
    Choices should have probabilities for different effects.
    For example:
    Player sees a ship in the horizon.
    1) Sail closer to the ship. (90% chance to trigger merchant ship event and 10% chance to trigger pirate ship event)

    I was thinking about creating these events as scriptable objects, so I can easily create them inside the editor.
    I tried with json file, but it quickly became very hard to read and to write new events.

    This is my structure so far:
    Code (CSharp):
    1. [CreateAssetMenu]
    2. public class RandomEvent : ScriptableObject
    3. {
    5.     public string text;
    6.     public List<RandomEventChoice> choices;
    8. }
    10. [System.Serializable]
    11. public class RandomEventChoice
    12. {
    13.     public string text;
    14.     public List<RandomEventAction> actions;
    15. }
    17. [System.Serializable]
    18. public class RandomEventAction
    19. {
    20.     public int probability;
    21.     //List of functions to execute, for example increase food by 100
    22. }
    I was thinking about adding somekind of resource manager object here and then choosing the function to execute from that, but it seems to not be possible with scriptable objects.
    Is it possible to do this somehow with scriptable objects? Or what would be the best approach?
  2. spiney199


    Feb 11, 2021
    Use Scriptable Objects as your delegates instead. As in, have a base scriptable class, and use derived types to do specific actions. Just have a collection of these as your actions.

    Alternatively, SerializeReference + some custom inspector work can let you do this with plain C# objects.