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

strange resides in a prefab error

Discussion in 'Scripting' started by rainbow_design, Jul 8, 2017.

  1. rainbow_design

    rainbow_design

    Joined:
    Aug 8, 2015
    Posts:
    108
    Hello, i upgraded to the recent version and i get a load of errors now stating:

    "Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption."

    I got this strange errors before and i did never really figure out why, however now i get the all over the code.


    Here is one example. infofield is not in a prefab as far i can see. It is a normal gameobject in the scene. Same for its parent.


    Code (CSharp):
    1.  
    2.         static public void dinfostart() {
    3.             pre ("dinfostart");
    4.             dprp ();
    5.             foreach (ip item in ipl) item.clear ();
    6.             infofield1 = go.Instantiatesame (infofield, false);  // <<<<<<<<<<<<<<<<<<<<<<<<<<<<< HERE IS THE ERROR >>>>>>>>>>>>>>>>>>>>>>>>>>>>
    7.             //infofield1.transform.position = g.nullv;
    8.             infofield1.name = "infofield" + 0;
    9.             h.getobjbn ("infofield" + 0).SetActive (true);
    10.             go.oldinfo = go.infotext;
    11.             filt = null;
    12.             infom ("gameset");
    13.             infom ("All").GetComponent<Button> ().onClick.AddListener (delegate { dinfo (gs.globaltemplate, null); });
    14.             infom ("Buildings").GetComponent<Button> ().onClick.AddListener (delegate { dinfo (gs.globaltemplate, p.Combine ("building", "")); });
    15.             infom ("Research").GetComponent<Button> ().onClick.AddListener (delegate { dinfo (gs.globaltemplate, p.Combine ("gameset", "research")); });
    16.             infom ("Maps").GetComponent<Button> ().onClick.AddListener (delegate { dinfo (gs.globaltemplate, p.Combine ("gameset", "maps")); });
    17.             infom ("game");
    18.             infom ("Researched").GetComponent<Button> ().onClick.AddListener (delegate { dinfo (g.ownerlist["player"].researched, null); });
    19.             infom ("Researchobj").GetComponent<Button> ().onClick.AddListener (delegate { dinfo (g.ownerlist["player"].resobj, null); });
    20.             //infom ("buildings").GetComponent<Button> ().onClick.AddListener (delegate { dinfo (); });
    21.             //gs.globaltemplate[sourcefile]
    22.         }
    23.  
    24.  
    25.         public static GameObject Instantiatesame(GameObject toinstantiate, bool worldpos = true) {
    26.             Transform oldparent = toinstantiate.transform.parent;
    27.             GameObject o = Instantiate (toinstantiate);
    28.             o.transform.SetParent (oldparent, worldpos);
    29.             prinstantiate ("same " + toinstantiate);
    30.             return o;
    31.         }
    Code (CSharp):
    1. Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption.
    2. UnityEngine.Transform:SetParent(Transform, Boolean)
    3. go:Instantiatesame(GameObject, Boolean) (at Assets/scripts/execute.cs:1931)
    4. m:dinfostart() (at Assets/scripts/execute.cs:897)
    5. UnityEngine.EventSystems.EventSystem:Update()
    6.  
     
  2. BTCallahan

    BTCallahan

    Joined:
    Jun 6, 2015
    Posts:
    71
    Does it still happen if you comment out line 28 (the "o.transform.SetParent")?

    After looking over the code, I don't think you need that part. If memory serves, instantiating a prefab should instantiate its parent along with the prefab.
     
  3. rainbow_design

    rainbow_design

    Joined:
    Aug 8, 2015
    Posts:
    108
    I just tried it. It does not cause an error but it will have "mainscene" as parent - which will break my code.
     
  4. BTCallahan

    BTCallahan

    Joined:
    Jun 6, 2015
    Posts:
    71
    Huh. That is not what I expected. In the asset (or project) window, does the prefab you are instantiating have a parent?
     
  5. rainbow_design

    rainbow_design

    Joined:
    Aug 8, 2015
    Posts:
    108
    yes it has but unity initiates new objects with the mainscene as parent (its just the way unity works) thats why i made Instantiatesame in the first place.
     
  6. rainbow_design

    rainbow_design

    Joined:
    Aug 8, 2015
    Posts:
    108
    Is there a way to be sure it not accidentally created a prefab from my scene gameobjects?
    I am not really sure how to interpret the error... i still think it might be a bug. Any suggestions?
     
  7. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    Are you aware that the Instantiate function already lets you instantiate and set the parent?

    You just simply use this overload:
    Code (csharp):
    1. public static Object Instantiate(Object original, Transform parent);
    Also, I got the impression you were already doing this, but ensure that you're parenting to something in the scene. You reference to the game object being parented to needs to be the instance.. not just to a prefab... i.e., when you actually drag it into the property inspector, you need to drag it from the scene. The alternative is when you instantiate the parent, store the instance in a variable, and then use that to parent to.

    Outside of those methods, you could have your two objects have their parent/child relationship as a prefab, and instantiate the parent (or possibly the child.. can't confirm what BTC said about instantiating the child automatically instantiating the parent is true, im not sure)

    The other method, I don't really recommend, is to find your parent in the scene, then parent to that instance. That may not work well, if there's multiple instances of the child and you want a 1:1 parent/child relationship.. it would require a little extra work to ensure you don't parent to something that already has another instance under it.
     
    Last edited: Jul 9, 2017
  8. rainbow_design

    rainbow_design

    Joined:
    Aug 8, 2015
    Posts:
    108
    I found a solution to this however its for me unexpected.
    I took a closer look at the prefab in the scene. While it was not marked as prefab it had "prefab" in its name.
    i renamed it to "pref" now it works like intended. However i am not sure if it works now because of the new name or because it was changed in the renaming process.