Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unique scripts needed for different levels?

Discussion in 'Game Design' started by Distraxxing, Nov 14, 2023.

  1. Distraxxing

    Distraxxing

    Joined:
    Apr 4, 2021
    Posts:
    1
    Hi, I have a query relating to general game design, as opposed to a specific code query.

    I've seen numerous posts with people asking how to preserve data throughout the game, and they are normally told to use DontDestroyOnLoad etc. However, I have the opposite problem. I need each level to be different, but any modification to any script affects all levels - i.e. they are all using the same scripts.

    For example, maybe I want an object to spawn randomly from an array of 4 on level 1, then from an array of 6 on level 2, and maybe not spawn at all on level 3; but if I modify the GameManager script in level 2 or 3, it is also modified on level 1 when I go back to it.

    Do you need a different version of every script for every level, or is there another way to manage it?


    Thanks
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    The differences in behavior on the different levels should be in the data of your scripts, not the code.

    The easiest way to do this is to declare public properties in your GameManager script. Then, you place this script on an object in each scene. In the Inspector, you'll find a field for each of those public properties, where you can define a value — it doesn't have to be the same as the default value set in the script. These values are stored in the scene (not with the script), so they can be different for every scene.
     
    Distraxxing and CodeSmile like this.
  3. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,575
    If you do, you do. If you don't, you don't.

    Nothing in your example requires different scripts, though.