0
\$\begingroup\$

I'm making a game with Unity. I wanted the player script to not have some features in some scenes but couldn't remove them from the script because I needed them for other scenes.

So far, I just unpacked those player prefabs, created new scripts for each of them and removed the lines that I didn't want to have. I know this was not the best idea but I don't know any other way. One of its problems is that it makes managing the scripts hard and confusing.

Is there a way that I can change them without creating new ones? Since I'm going to switch to the new Input system, I want to avoid the possible problems and headaches around these scripts. I also did this (creating a new script) for pause menu script as well. I provided two examples below to make the situation more clear.

Example A) The player can use a shotgun in one of the scenes and I chose to not let him to use the knife in there. So I created a new script for that player and copied and pasted the original script (which had the melee attack ability) and then removed those lines from the new script.

Example B) Pause menu can disable the health bar game object whenever the escape button is pressed. But for one of the levels, I don't want to use the health bar, so I pasted the code in a new pause menu script and removed the line that disables the bar.

\$\endgroup\$
3
  • \$\begingroup\$ Instead of copying everything else it is better to do the opposite. Separate the optional part in a different MonoBehaviour and add it only in the scenes you want. For example make a component that disables the healtbar when pause is active and don't use it in scenes that you want health to remain (or does not exist). In other words try to not mix what a single MonoBehaviour does - pause menu should deal with its own stuff and shouldn't have to care about healthbars. Yes you will end with more components to manage but you will be able to easily combine them for different end results. \$\endgroup\$
    – Nikaas
    Mar 25 '21 at 7:51
  • \$\begingroup\$ @Nikaas that looks like it would make an upvote-worthy Answer, if you'd like to post below instead. \$\endgroup\$
    – DMGregory
    Mar 25 '21 at 11:40
  • \$\begingroup\$ @DMGregory I will post it, I was not sure if it was detailed enough. \$\endgroup\$
    – Nikaas
    Mar 25 '21 at 16:25
3
\$\begingroup\$

Instead of copying everything else it is better to do the opposite. Separate the optional part in a different MonoBehaviour and add it only in the scenes you want.

For example make a component that disables the healtbar when pause is active and don't use it in scenes that you want health to remain (or does not exist). In other words try to not mix what a single MonoBehaviour does - pause menu should deal with its own stuff and shouldn't have to care about healthbars. The same way you can separate your abilities in different MonoBehviours and add the ones you want per level.

Yes you will end with more components to manage but you will be able to easily combine them for different end results.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ I'd flip your health bar example around. Make a component that shows the health bar, and omit that component when you don't want it shown. 😉 \$\endgroup\$
    – DMGregory
    Mar 25 '21 at 16:29
  • \$\begingroup\$ @DMGregory Of course, lol. \$\endgroup\$
    – Nikaas
    Mar 25 '21 at 16:46
  • \$\begingroup\$ Thank you both for your help! I deleted the duplicates and created new scripts specifically for the features that I don't want to use in some scenes. So far, I just completed the health bar set active script which works properly. The shotgun script has some bugs and I'm trying to fix them but will ask a new question if I encounter a problem. \$\endgroup\$
    – Ali
    Mar 26 '21 at 3:23
  • \$\begingroup\$ @DMGregory I just have a question which is related to this problem. My question is do I need to keep the objects that I want to change as prefabs in all scenes but without overriding them or is it better to have them unpacked, since this way they can be changed easier? \$\endgroup\$
    – Ali
    Mar 26 '21 at 3:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .