Search Unity

Can not drag a GameObject into Script that is attached to Prefab because Hierarchy is not displayed!

Discussion in 'Prefabs' started by Scorfa, Mar 4, 2019.

  1. Scorfa

    Scorfa

    Joined:
    Apr 27, 2017
    Posts:
    3
    upload_2019-3-4_19-41-28.png

    I cannot drag a GameObject into the Field of the Script because I cannot access the Hierarchy.
    If I go to the Hierarchy then the Prefab in the Inspector closes.
    Sry for this dumb question, but I cant figure it out how to drag a GameObject from the Hierarchy onto my Prefab.
     
    shaheerl likes this.
  2. iMobCoding

    iMobCoding

    Joined:
    Feb 13, 2017
    Posts:
    165
    Why would you drag anything from the Hierarchy to the prefab? Once you instantiate that prefab, all references to those hierarchy objects would be lost
     
  3. Scorfa

    Scorfa

    Joined:
    Apr 27, 2017
    Posts:
    3
    I tried that but it also takes me away from the Hierarchy once I have the Prefab open.


    So for example:
    Every time I spawn a Soldier(Prefab) he should straight away walk towards a "Destination".
    In order for him to walk towards the destination I need to tell him what it is.
    Now I could make this work by having the following code on the Script that is attached to the Prefab:

    Code (CSharp):
    1. GameObject destination;
    2.     void Start()
    3.     {
    4.         destination = GameObject.Find("Destination");
    5.     }
    Now I was just thinking why you can not set the destination through the Inspector by using
    Code (CSharp):
    1. public GameObject dragedThroughInspectorDestination;
    If I remember correctly this was possible some time ago, and if it was... why would they change that?

    I did some research and came across some people explaining that it is not possible for a Prefab to reference a GameObject in a Scene.
    Here is one link:
    https://gamedev.stackexchange.com/q...-store-references-to-scene-objects-in-prefabs

    So I basically just have to accept that it is not possible^^
    Anyway thx for replying and helping!
     
    LiadIdan likes this.
  4. iMobCoding

    iMobCoding

    Joined:
    Feb 13, 2017
    Posts:
    165
    Didn't test with new prefab system, but yes, before, it was possible to drag an object from hierarchy to the prefab. But then, once you run the game, you experience the errors. It's actually good if they disabled this now
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    That wasn't possible in the old prefab system either. A prefab can't have references to objects in a scene.

    You could drag objects onto a prefab instance that's in the scene. But not into the prefab asset, which is what you're editing in the prefab view.
     
    JKrypto and Tonymotion like this.
  6. iMobCoding

    iMobCoding

    Joined:
    Feb 13, 2017
    Posts:
    165
    Yes, you're right. What I meant was that you could drag object to prefab instance in the scene and then apply changes to prefab. But those references would be lost
     
    Baste likes this.
  7. Scorfa

    Scorfa

    Joined:
    Apr 27, 2017
    Posts:
    3
    Exactly that.
    You first dragged a Prefab into a Scene. Then you made the reference you wanted with the Prefab. Applied the changes you made to Prefab for all Instances. And then you just removed the Prefab from the Scene. And after that you could create new Instances of the Prefab at Runtime that had the reference you wanted.

    Is that correct? Or is my memory going? ^^
    I am talking about 2 years ago or so.
     
    unity_TEkEHKcvu5sRAw likes this.
  8. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    I'm having this issue right now.
     
  9. GameDev2021

    GameDev2021

    Joined:
    Sep 3, 2019
    Posts:
    7
    Same issue here.
     
  10. ecv80

    ecv80

    Joined:
    Oct 1, 2017
    Posts:
    28
    This doesn't appear to work anymore (2019.2)
     
  11. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    You can drag an asset (like a Material) into an Object Field on a Prefab instance (like the Material Object Fields on a MeshRenderer), and if you apply that, the reference to the Asset (Material) in this case) is saved into the Prefab Asset. This was always the case and is still the case.

    If you drag an object in the scene into an Object Field on a Prefab, and apply, then one of two things happen:
    • If the object belongs to the Prefab instance itself (the same GameObject or a child) then it's saved into the Prefab Asset as an internal reference to a different part of the Prefab Asset.
    • If the object does not belong to the Prefab instance itself, then it won't be saved into the Prefab Asset. The instance will keep referencing the object in the scene, but on the Prefab Asset the Object Field won't have any object assigned.
    Again, this too was always the case and is still the case.
    If you're seeing something that does not follow the above, then please be more specific about exactly what you're doing, and file a bug report about (via the bug reporter, not a forum post) it if you want us to be able to look into it.
     
    xue-xue-xue likes this.
  12. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    Is it possible that the implementation was more naive at one point, and just tried writing the scene reference to the object, allowing it to be missing outside the context of the scene?

    If that was the case, then it would "work" to have references to objects in scenes on prefabs as long as you only instantiated the prefab in the same scene.

    I'm guessing that people were duplicating prefab instances and not realizing that the scene references were kept due to the duplication rather than the prefab being involved, but you never know.
     
  13. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    I doubt it. At least I just tested in Unity 3.4 from 9 years ago and it was the same back then as today.

    That's possible.
     
    Baste likes this.
  14. Blaze400

    Blaze400

    Joined:
    May 9, 2021
    Posts:
    2
    i know this is an old post but for anyone who has the same issue. Making the game object the child of the prefab can sometimes fix it under the right circumstances
     
    Vickey_Soni likes this.
  15. Peter_Olsted

    Peter_Olsted

    Unity Technologies

    Joined:
    Apr 19, 2021
    Posts:
    75
    If you have a project where it's broken, could you make a bug report on it?
    Just go to "Help"->"Report a Bug..." in Unity and follow the guide, it would help us a lot.
     
  16. SolomonSliwinski

    SolomonSliwinski

    Joined:
    Nov 3, 2016
    Posts:
    1
    I was having a similar issue, where I was attempting to drag a transform into a field with the SerializeField attribute. Both the transform and the field were part of the same prefab, but the editor would not assign the transform, and the object picker UI had no entries.

    It turned out the issue was that I had the inspector pane locked. After a bit of experimentation it looks like the way to get into this state is to:

    1. Select the prefab, but do not open it. This will put the top level game object into the inspector UI.
    2. Click the lock icon on the inspector.
    3. Click open prefab button
    4. Attempt to drag a transform from the prefab scene into the serialized field in the inspector.
    5. Notice that the inspector UI doesn't allow this.

    Possibly this behavior is by design, but it's a nice gotcha, so I thought I'd post in case other people run into this issue.
     
    CosPie likes this.
  17. ShadowLabz

    ShadowLabz

    Joined:
    Nov 16, 2021
    Posts:
    1
    bruh just lock the hierarchy
     
  18. Vickey_Soni

    Vickey_Soni

    Joined:
    Jun 9, 2022
    Posts:
    13
    Same Issue. Can not find any Solution
     
  19. Peter_Olsted

    Peter_Olsted

    Unity Technologies

    Joined:
    Apr 19, 2021
    Posts:
    75
    Hi, have either of you found any way to reproduce the issue?
     
  20. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    I mean they probably have reproduced it as this thread is about "x doesn't work" where x is something that never worked in the first place!

    Prefab assets cannot have references to scene objects. That's kinda fundamental. All the posters upthread ended up replying with a variation of "oh, my bad, this never worked". And then there's somebody that necros every year or so.
     
  21. Vickey_Soni

    Vickey_Soni

    Joined:
    Jun 9, 2022
    Posts:
    13
    That was not the issue, I was unable to drag any items in whole unity interface. when i try to drag any object or prefab, my cursor changes to a not allowed sign with 45 degree cross line. I started Unity after a few hours, and issue solved it self.
     
  22. Vickey_Soni

    Vickey_Soni

    Joined:
    Jun 9, 2022
    Posts:
    13
    Nope, it solved it self after starting my PC after a few hours. And I'm not facing this issue since then.