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

Can't work with Image component in code

Discussion in 'Scripting' started by AlexChanyan, Feb 15, 2020.

  1. AlexChanyan

    AlexChanyan

    Joined:
    Dec 15, 2017
    Posts:
    3
    Hi, I'm having trouble working with UI Images in 2019.3.0f6.

    I've tried using GetComponent and GetComponentInChildren but they can't get Image components in my code. I've also tried using SerializeField and public fields to try to set my Image in the editor, but it won't show up. Here's some code for both examples:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UIElements;
    3.  
    4. public class FadeToBlack : MonoBehaviour {
    5.  
    6.     [SerializeField] private UnityEngine.UIElements.Image image;
    7.     [SerializeField] private Image image2;
    8.  
    9.     public static void FadeToScene (string sceneName) {
    10.         GameObject prefab = Resources.Load<GameObject>("Prefabs/FadeToBlackUI");
    11.         GameObject go = Instantiate(prefab);
    12.         FadeToBlack ftb = go.GetComponent<FadeToBlack>();
    13.         ftb.image = go.GetComponentInChildren<UnityEngine.UIElements.Image>();
    14.     }
    15. }
    This code doesn't provide a field in the editor to add the Image to. When calling the function, it gives me the following error on the
    ftb.image = go.GetComponentInChildren<UnityEngine.UIElements.Image>();
    line:

    ArgumentException: GetComponent requires that the requested component 'Image' derives from MonoBehaviour or Component or is an interface.

    Any help with solving this problem would be appreciated. Thanks.
     
  2. mustafatat

    mustafatat

    Joined:
    Oct 1, 2017
    Posts:
    6
    Would this be useful, I don't know. I hope it happens.

    Code (CSharp):
    1. public class FadeToBlack : MonoBehaviour
    2. {
    3.     public UnityEngine.UIElements.Image image;
     
  3. AlexChanyan

    AlexChanyan

    Joined:
    Dec 15, 2017
    Posts:
    3
    I tried that after noticing SerializeField didn't work, but setting it as public didn't work either.
     
  4. TheGameNewBie

    TheGameNewBie

    Joined:
    Jul 27, 2017
    Posts:
    92
    I'm not sure what you are trying to do in the code.
    Are you displaying an image inside the game?
    Or are you creating a UI Window like the Inspector?

    From my understanding, UIElements is used to make custom Editor Windows.
    If you want to Display an Image in game, You need to use Unity UI.
    You need to import the UI Package from package manager.
     
    Last edited: Feb 16, 2020
    xXMadWolfXx likes this.
  5. AlexChanyan

    AlexChanyan

    Joined:
    Dec 15, 2017
    Posts:
    3
    Thanks, this seems to have been the problem.

    I already had the UI package in my project but for some reason my visual studio isn't recognizing UnityEngine.UI, so I assumed it was just replaced with UIElements.

    I've got it working now.
     
    pren1 and demalpica like this.
  6. demalpica

    demalpica

    Joined:
    Jan 30, 2017
    Posts:
    7
  7. Visuallization

    Visuallization

    Joined:
    Mar 14, 2015
    Posts:
    8
    Thanks this worked for me!

    If anyone else is having troubles with this, like I did, because I thought the package needs to be updated in vscode instead of unity. Here is the exact way to fix this (was already mentioned in the linked post but I missed it quiet a few times).

    1] In Unity, go to Window/Package Manager
    2] Search for your Visual Studio Code Editor package
    3] Upgrade to version 1.2.3
    4] Close Unity
    5] Delete your .csproj and .sln files in your project
    6] Open Unity

    cheers
     
  8. pren1

    pren1

    Joined:
    May 9, 2021
    Posts:
    4
    That answered my problem. One should use UnityEngine.UI instaed of UIElements