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

Changing Scripts Between Levels

Discussion in 'Scripting' started by _Trap_, Nov 19, 2016.

  1. _Trap_

    _Trap_

    Joined:
    Nov 19, 2016
    Posts:
    2
    I am creating a block breaker type game and I want the launch speed of the ball to be different in different levels. How would I use the same script but only change a portion of it in a different level?
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Put it on a new GameObject in the new scene. Configure it differently via the inspector.
     
    _Trap_ likes this.
  3. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,797
    I'm a big fan of keeping game config in a JSON file, and keeping it on a server.
    Allowing you to tweak the config without having to update any code, and loading it from the server when the game loads.

    If not using a server, you can read a JSON file from StreamingAssets.
    Putting config in JSON allows you to easily tweak a lot of values at once.

    For instance, if you have 20 levels, and want to slightly increase the ball speed for each level, its easier to quickly edit a JSON file, than open every scene and change each GameObject individually.

    An even better way to do it, is create a config generator class, that uses base values and offset values for the ball speed on all your levels to instantly generate your JSON config.

    It really depends how often you will be tweaking values and fine tuning your game.

    But @BoredMormon's solution is nice and simple if you don't want to get too heavy into any details and only have a few levels :)
     
    _Trap_ and Kiwasi like this.
  4. _Trap_

    _Trap_

    Joined:
    Nov 19, 2016
    Posts:
    2
    How do I do that, It isn't a public variable that I can edit with the inspector.
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Expose it to the editor with [SerialiseField]