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. Dismiss Notice

Question Nested prefab is automatically overwritten

Discussion in 'Prefabs' started by munkbusiness, May 24, 2023.

  1. munkbusiness

    munkbusiness

    Joined:
    Aug 22, 2017
    Posts:
    55
    Hey I have a prefab: "BattleCharacterUI", which inside of it has another prefab that is able to instantiate new "BattleCharacterUI"'s.

    I noticed that the sub prefab had a reference to the its own parent instead of the prefab version, as I was debugging to figure out why new instance of "BattleCharacterUI" kept some of the changes made during runtime of previously Instantiated ones.

    If Iclick revert all, the reference is changed back, but if I then exit the prefab editing window and open up the prefab again, the reference is changed right back.

    Is it just a visual bug or what is going on? Thank you in advance.

    unity_forum_help.png
     
  2. munkbusiness

    munkbusiness

    Joined:
    Aug 22, 2017
    Posts:
    55
    Update: For now until I get an answer on how to deal with this, I have chosen to Instantiate the "TimeSlot" objects instead og changing their values. Before I would get a loop that created endless Instantiations of itself, because it was self referential, that would flood memory. But it is annoying that I have to make the performance worse by instantiating the "TimeSlot"-prefabs instead of just changing their fields.
     
  3. Peter_Olsted

    Peter_Olsted

    Unity Technologies

    Joined:
    Apr 19, 2021
    Posts:
    74
    Hi, when editing the content of a Prefab, references to Prefab you are editing will change to the icon of a normal Prefab, because internally Unity unpacks the Prefab. So the icon change is expected not indicating any wrong behavior.
    If you edit a Prefab while the game is running all changes are permanent.

    I am unsure if these answers fully explains the behavior you are see however. If it doesn't could you create some steps to reproduce the issue?
     
  4. munkbusiness

    munkbusiness

    Joined:
    Aug 22, 2017
    Posts:
    55
    This is the structure

    Prefab A
    -Prefab B (child of A)
    --Prefab C (Child of B) -> Can instantiate Prefab A

    What I do is:
    Instantiate Prefab A, then run some Init() functions on B and C that changes some text, images, ect. Then C can in Instantiate a new Prefab A in certain situations.

    The error I described is:
    The new instantiated Prefab A keeps all the changes in its children Prefabs (Prefab B and Prefab C) instead of what I assumed would be a new clean slate.

    Basically how I assumed it worked was, that instantiating Prefab A also would bring new Prefab B and C with it. That didnt seem to be the case.

    I assume that the issue is that it is creating a reference to itself (picture above) instead of a project level Prefab. You say that is is entirely visual, which seems wierd to me since it registers as a change that I can revert.

    I hope this explains the issue better.