Search Unity

Question on scripts when doing multiple level in games

Discussion in 'Editor & General Support' started by Deleted User, Nov 5, 2019.

  1. Deleted User

    Deleted User

    Guest

    I completed my basic Unity online course recently. I am now practising on my own. I have enough code from another question that, once added, automatically enables my game to be used on smartphones rather than on the desktop only, so the learning curve is fast. My question is a very basic and it doesn't involve comprehensive technical knowledge in Unity or c# programming:

    When software developers develop games, to they write multiple scripts in different levels OR are all scripts in one file only? Let me explain what I'm trying to accomplish: The following is a common scenario. Lauch game -> splash screen, then main screen -> choose difficulty -> level 1 of respective level. My question is do we code all different levels separately in different c# script files or put them all in ONE file? Or it is my choice? Is one choice more professional or possibly better than another?
     
    Last edited by a moderator: Nov 5, 2019
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    In general, if your different levels involve mostly common code, you'll have a single script to avoid unnecessary code duplication across several scripts. If the different levels have nothing in common, then you're more likely to want them in separate scripts.

    For example, if the differences between level 1 and level 2 are just higher difficulty and some enemies and terrain are different, but the game mechanics are the same, you'd generally use the same scripts for both levels. On the contrary, if level 1 was a pinball game, and level 2 was a tennis match, you're likely to be using a lot of different scripts for each respective level.

    Generally you include functionality in the same script which logically goes together. If your game is some kind of shooter, you're likely to have a health script on the player and all the enemies which handles everything related to health (taking damage, healing, exposing a health variable for use by a separate health UI script, etc). You're also likely to have some kind of weapon script which handles most everything related to loading and firing the weapon. Often you'll also have a separate player input script which takes input for various actions, and calls methods on other scripts to do those actions (this allows you to reuse the weapons script on both players and AI, because the weapons script doesn't care what action lead to telling it to fire the weapon - it could be a player input script, or an AI script telling it to fire, it doesn't know or care).
     
  3. Deleted User

    Deleted User

    Guest

    Thank you.