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 How do I drag prefab into image space in the inspector?

Discussion in 'Editor & General Support' started by faulknordonald, Sep 22, 2023.

  1. faulknordonald

    faulknordonald

    Joined:
    Sep 9, 2019
    Posts:
    115
    This used to be possible, but Unity took away this option. I realize I may have to modify my code.

    In a tutorial video from 5 years ago, they drag a prefab into an image space in the inspector.

    See the image for reference.

    upload_2023-9-22_0-18-10.png

    Here's the current code for this particular task:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6.  
    7. public class GoalPanel : MonoBehaviour
    8. {
    9.     public Image thisImage;
    10.     public Sprite thisSprite;
    11.     public TextMeshProUGUI thisText;
    12.     public string thisString;
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         Setup();
    18.     }
    19.  
    20.     void Setup()
    21.     {
    22.         thisImage.sprite = thisSprite;
    23.         thisText.text = thisString;
    24.     }
    25. }
    26.  
    I would appreciate any assistance you can provide. Thank You.
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Is this prefab trying to reference itself?

    You should just be able to drag via the image component's header.
     
  3. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,105
    Are you sure there is an Image component attached to Goal Images game object?

    Dragging-and-dropping like that does still work, as long as there is a component of a matching type attached to the dragged object.
     
    spiney199 likes this.
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Yeah looking at the image again, you're inspecting
    Goal Prefabs
    , which does have an image component. While you're trying to drag
    Goal Image
    into the field, which likely doesn't have the right component on it.

    You might've put the component on the wrong game object then?
     
    SisusCo likes this.
  5. faulknordonald

    faulknordonald

    Joined:
    Sep 9, 2019
    Posts:
    115
    Ok, so, I was able to select "Goal Image" from the box by selecting the little circle, but it says, "Type Mismatch".

    upload_2023-9-22_11-3-40.png
     
  6. faulknordonald

    faulknordonald

    Joined:
    Sep 9, 2019
    Posts:
    115
    UPDATE: After switching Unity versions, it's working. But, why would Unity remove such a feature in newer versions?
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    They didn't. You can always drag Components into fields.

    Some possibilities that are FAR more likely to have happened:

    - you have a compiler error in the newer Unity

    - you are not dealing with the same
    Image
    class type that you think you are (be explicit if you mean UnityEngine.UI.Image and you have other Images)

    - you used UIElements instead of UI

    etc etc etc
     
  8. faulknordonald

    faulknordonald

    Joined:
    Sep 9, 2019
    Posts:
    115
    I'm not the only one to have the same issue with the exact same situation (viewers of the tutorial). There were no compile errors. The code was written exactly as the tutorial (and no errors in console).

    Simple fact is, the 2022 version of Unity does not allow you to drag a prefab game object into an Image component from what I've determined. At least not in this case.

    Image prefab, for some reason, can't be dragged into this in the inspector in Unity 2022:

    Code (CSharp):
    1. public Image thisImage;
    But, it worked in Unity 2020.
     
  9. faulknordonald

    faulknordonald

    Joined:
    Sep 9, 2019
    Posts:
    115
    Here's the video. It will play precisely where he drags the prefab into the inspector:
    .

    If you read the comments by: kellysharma1113

    You will see others having the same issue.
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Everything works as expected for me. Here's what I just tested:

    install and open 2022.3.10f1:
    - make a new project
    - make a script with a public UnityEngine.UI.Image field
    - make a Canvas with an Image in it
    - drag the GameObject with that Image into the field - WORKS.

    Goto my 2021 project:
    - made a prefab of a Canvas with Image, saved the prefab
    - closed 2021, dragged the saved prefab from the 2021 project to the 2022 project
    - dragged the prefab into my scene
    - dragged the GameObject Image into my script field - WORKS

    Repeated ALL the above with an old 2018 project - WORKS.

    Took the 2018 project, upgraded it in place to 2022:
    - fixed compiler errors related to Package crapware
    - newly-created Image instance: WORKS
    - old prefab from when it was a 2018 project dragged into slots - WORKS

    We use this every day at 100+ person production scale. We're migrating to 2022 now. My team is on 2021, but other teams are on 2022.

    This stuff does work.

    I'm still guessing you have compiler errors, or else perhaps you're not being explicit with your type and you have something else called Image.



    Make sure your log console selector buttons are enabled. See this graphic:

    https://forum.unity.com/threads/cant-add-script-component.632746/#post-4239121

    https://forum.unity.com/threads/update-function-not-working.953477/#post-6215873




    Extra unwanted packages in new projects (collab, testing, rider and other junk):

    https://forum.unity.com/threads/temp-unityengine-testrunner-dll-error.1133938/#post-7287748

    About the fastest way I have found to make a project and avoid all this noise is to create the project, then as soon as you see the files appear, FORCE-STOP (hard-kill) Unity (with the Activity Manager or Task Manager), then go hand-edit the Packages/manifest.json file as outlined in the above post, then reopen Unity.

    Sometimes the package system gets borked from all this unnecessary churn and requires the package cache to be cleared:

    https://stackoverflow.com/questions/53145919/unity3d-package-cache-errors/69779122
     
  11. faulknordonald

    faulknordonald

    Joined:
    Sep 9, 2019
    Posts:
    115
    Definitely wasn't compile errors. It's always the first thing I check. In your 2022 example, it doesn't say that you made the game object or canvas into a prefab. In your 2021 example, it says you took the prefab over to your 2022 version. And you said it works with 2018, which I already knew it would with an older version. Did you try make the 2022 version game object into a prefab and dragging it from there? Or was the only workaround to make it in 2021 and take it over to 2022?
     
  12. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    The prefab examples were Canvas with Image under it.

    Of course after copying it I dragged it into scene because you cannot just drag a sub-child part of a prefab into a scene. That's never been possible, even before the nestable prefabs.

    However, as you requested above, in 2022 I made a bare prefab out of the Image without the Canvas (a VERY questionable workflow!) and I can drag that Image into a scene, then drag it into my field all day long.

    I can also drag that bare Image prefab into the in-scene GameObject's field.

    You will NEVER be able to drag a Canvas into an Image slot, no matter how many Images are underneath the Canvas on other GameObjects. That's never been a thing.
     
  13. faulknordonald

    faulknordonald

    Joined:
    Sep 9, 2019
    Posts:
    115
    I know that. Maybe you misunderstood. I was trying to drag a BLUE (because it's a prefab) Image GameObject into the Image spot. I never tried dragging the canvas. But, in 2022, it wouldn't allow me to drag the blue Image to an Image spot in the inspector. After I switched to 2020, it allowed me to. Nothing else was changed besides switching versions.
     
  14. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    There's been no change. You were just dragging a game object that didn't have an Image component on it. Simple as that.

    The feature still works. I was using it yesterday in Unity 2022 and 2023.
     
  15. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,105
    Could it be that you had the Inspector window locked in your Editor?

    In your first screenshot the Inspector has the message
    Root in Prefab Asset (Open for full editing support)
    , which seems to indicate that you were inspecting the prefab asset selected in the Project window, instead of the prefab instance that can be selected in the Hierarchy window.

    So when you tried to drag a reference from the unpacked instance in the prefab stage to the prefab asset, Unity doesn't allow it, since scene-to-prefab references aren't supported.


    But then I'm not sure how you were able to select the Image using the Object Picker, and getting the
    Type mismatch
    issue.

    Perhaps this isn't supported for assigning references from the children of a prefab asset? Not sure if I've ever tried that.

    Could also be the editor somehow got into a wonky state from the prefab being simultaneously modified by two sources (prefab stage with auto saving enabled and the locked inspector).