Search Unity

Bug Prefab settings overwritten during runtime???

Discussion in 'Prefabs' started by deLord, Jan 30, 2021.

  1. deLord

    deLord

    Joined:
    Oct 11, 2014
    Posts:
    306
    Hey guys

    I hope this can be replicated somehow: I recently added a line that instantiates a prefab from the Resources folder, into which I had to move my prefab assets as well.
    What happens is that when playing the game, the settings in my prefab (for example an int rotState) are changed PERMANENTLY. I have never seen this before and think under no circumstances should this happen. It's always 0 in the prefab and becomes 3 when ending game mode.

    Code (CSharp):
    1. Destroyer dstr = Instantiate(Resources.Load("Prefabs/Destroyers/myPrefab"), sometransform) as Destroyer; // happens here
    2. GameObject dstrGO = (GameObject) Instantiate(Resources.Load("Prefabs/Destroyers/myPrefab"), sometransform); // and here
    Now I moved them back to their original folder, and instead of using Resources.Load() I set a variable via Editor. Still: they get changed all the time. What the f*ck guys. This is in the truest sense game-breaking. I cannot continue working anymore because everytime my prefabs are changed. Maybe I should add that this concerns prefab variants. I reverted all my code but still... I rotate the clone and the prefab changes

    prefabwtf.jpg

    Using 2019.4.18f1 LTS

    Quick help much appreciated!
     
    Last edited: Jan 30, 2021
  2. deLord

    deLord

    Joined:
    Oct 11, 2014
    Posts:
    306
    OK, although I thought this was impossible and should not happen, here is what I finally figured.

    I have a button-class that does
    Code (CSharp):
    1. player.selectedDestroyer = destroyerPrefab; // prefab set via Editor
    This is a reference to the prefab obviously.
    Now when the player rotates this variable via selectedDestroyer.rotate() it actually calls this method on the very prefab element.

    Bug or feature ... I'd say bug, because I would not expect to totally screw your prefabs from within the game ô.O
     
    Last edited: Feb 1, 2021
    ssellvf likes this.
  3. deLord

    deLord

    Joined:
    Oct 11, 2014
    Posts:
    306
    nobody interested?
    Unity team?
     
  4. deLord

    deLord

    Joined:
    Oct 11, 2014
    Posts:
    306
  5. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,188
    Well, you're modifying the prefab asset instead of the game instance, so that's what you get. I don't think there's anything sleek and easy that Unity can do to prevent this mistake since they shouldn't make it impossible to modify source assets because of editor code. I'd argue against investing in systems that somehow reset all prefabs after play mode since that just wouldn't be feasible performance-wise. Also, again, there's editor code that may be run intentionally, so Unity really doesn't know if you want to modify your prefab or only an instance.