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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Prefabs are altered during playmode?

Discussion in 'Scripting' started by turbosheep, Mar 28, 2017.

  1. turbosheep

    turbosheep

    Joined:
    Jul 19, 2013
    Posts:
    25
    Alright, so I have a prefab in my scene. It has custom monobehavior script attached.
    During playmode (editor), the prefab has some values changed to this script.
    The object also gets deactivated during the process.

    Normally, you'd expect all these changes to revert once I stop testing in playmode.
    Problem: they don't. I have no idea idea why this happens, or what in my code might cause this.

    So... does anyone have any idea why my prefabs get saved during playtime, and how I can prevent this from happening? Thanks in advance!
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Prefabs are an asset. So if you make changes directly to the asset, they will persist. It's only scene objects that revert.
     
  3. turbosheep

    turbosheep

    Joined:
    Jul 19, 2013
    Posts:
    25
    Really? Can't believe I didn't know that. Thank you so much.

    Question. I want to have prefabs in order to quickly edit objects over multiple scenes.
    I have a BGM prefab for instance - if I change the music, I want to edit the BGM prefab and click apply without editing multiple scenes.

    But I also want to test the game without actually altering them.
    What's the usual way to achieve this?
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You can create a scene object that everything references, and keep it around with DontDestroyOnLoad.
     
  5. turbosheep

    turbosheep

    Joined:
    Jul 19, 2013
    Posts:
    25
    Thanks for the tip!