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

Easiest way to make difficulty work with Listeners?

Discussion in 'Scripting' started by KoDan76, Nov 22, 2022.

  1. KoDan76

    KoDan76

    Joined:
    Apr 29, 2019
    Posts:
    1
    This is my first project in unity.
    I'm making a simple 2D shooter game. I'm planning to be able to increase difficulty via a slider in the opitions menu. The slider will affect the opponents health possitively and the player's health negatively. I already figured out how to load and save the slider's value, but I'm a little uncertain about listeners. How am I supposed to implement a trigger that trigger when I hit my Okay button in options. It should change the health value in all the monoscripts.
    Or should I just extract raw value from the database?
     
  2. dynamicbutter

    dynamicbutter

    Joined:
    Jun 11, 2021
    Posts:
    61
    The class that saves and loads your games config values can contain the Unity event that the listeners are listening to. Anytime the class performs a load or save it can call the Invoke method of the Unity event to notify all listeners that the values may have changed.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,717
    Wow man, don't get ahead of yourself with complications!!!!

    Just keep it simple, get it working first.

    - connect the slider to a persistent property. Your description sounds like it would be a GameManager type construct, or perhaps a player preferences type construct.

    - use the value within your game to change the behavior

    That's it. Don't start focusing on "listeners" and "databases"... it would be like worrying about what type of air the gas station is using to inflate your tires. Who cares? Just go drive.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,513
    This isn't a post for the 2D forum. Please use the Scripting forum or the feature specific forums and not the 2D forum because you're making a 2D game.

    That way, this forum can stay focused on 2D specific issues and questions.

    Thanks.