c# - Image not showing when being instantiated - Stack Overflow

admin2025-04-26  3

I have a script in unity that makes a copy of an image in a level editor system that creates a translucent copy where the mouse is, for some reason the translucent copy is not showing up but in the inspector it is enabled and everything is the same as the other image apart from the alpha value. The code is here:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

[System.Serializable]
public class SlotPrefab
{
    public string slotName;
    public GameObject slot;
    public GameObject prefab;
}

public class Editor : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
    public List<SlotPrefab> slotPrefabList = new List<SlotPrefab>();

    public GameObject panel;

    private bool isDragging = false;
    private SlotPrefab hologramSlot = null;
    private GameObject slotHologram = null;

    void Update()
    {
        if (isDragging && slotHologram != null)
        {
            RectTransform rect = slotHologram.GetComponent<RectTransform>();
            rect.position = Input.mousePosition;
            rect.position = new Vector3(rect.position.x, rect.position.y, 0f);
        }
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        foreach (SlotPrefab slot in slotPrefabList)
        {
            if (eventData.pointerCurrentRaycast.gameObject == slot.slot)
            {
                isDragging = true;
                slotHologram = (GameObject) Instantiate(slot.slot);
                slotHologram.name = "Hologram";
                slotHologram.transform.SetParent(transform);
                hologramSlot = slot;
                SetHologramTransparency(slotHologram, 0.5f);
                break;
            }
        }
        
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        if (slotHologram != null)
        {
            isDragging = false;

            RectTransform panelRect = panel.GetComponent<RectTransform>();
            RectTransform hologramRect = slotHologram.GetComponent<RectTransform>();

            float panelRightEdge = panelRect.position.x + panelRect.rect.width / 2f;
            float hologramLeftEdge = hologramRect.position.x - hologramRect.rect.width / 2f;

            if (hologramLeftEdge <= panelRightEdge)
            {
                Destroy(slotHologram);
            }
            else
            {
                GameObject newThing = (GameObject) Instantiate(hologramSlot.prefab);
                newThing.transform.SetParent(null);
                newThing.transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                newThing.transform.position = new Vector3(newThing.transform.position.x, newThing.transform.position.y, 0f);
            }


            hologramSlot = null;
            slotHologram = null;
        }
    }

    private void SetHologramTransparency(GameObject hologram, float alpha)
    {
        Image image = hologram.GetComponent<Image>();
        if (image != null)
        {
            Color color = image.color;
            color.a = alpha;
            image.color = color;
        }
    }
}

I tried locating the issue but nothing seemed to happen. It is meant to make a transparent object when I click on one of the slots that follows the mouse until it is released. Then if it is on the panel it should disappear and do nothing, but otherwise it should disappear but instantiate a prefab.

Everything else works though it is just I can't see the hologram.

I have a script in unity that makes a copy of an image in a level editor system that creates a translucent copy where the mouse is, for some reason the translucent copy is not showing up but in the inspector it is enabled and everything is the same as the other image apart from the alpha value. The code is here:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

[System.Serializable]
public class SlotPrefab
{
    public string slotName;
    public GameObject slot;
    public GameObject prefab;
}

public class Editor : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
    public List<SlotPrefab> slotPrefabList = new List<SlotPrefab>();

    public GameObject panel;

    private bool isDragging = false;
    private SlotPrefab hologramSlot = null;
    private GameObject slotHologram = null;

    void Update()
    {
        if (isDragging && slotHologram != null)
        {
            RectTransform rect = slotHologram.GetComponent<RectTransform>();
            rect.position = Input.mousePosition;
            rect.position = new Vector3(rect.position.x, rect.position.y, 0f);
        }
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        foreach (SlotPrefab slot in slotPrefabList)
        {
            if (eventData.pointerCurrentRaycast.gameObject == slot.slot)
            {
                isDragging = true;
                slotHologram = (GameObject) Instantiate(slot.slot);
                slotHologram.name = "Hologram";
                slotHologram.transform.SetParent(transform);
                hologramSlot = slot;
                SetHologramTransparency(slotHologram, 0.5f);
                break;
            }
        }
        
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        if (slotHologram != null)
        {
            isDragging = false;

            RectTransform panelRect = panel.GetComponent<RectTransform>();
            RectTransform hologramRect = slotHologram.GetComponent<RectTransform>();

            float panelRightEdge = panelRect.position.x + panelRect.rect.width / 2f;
            float hologramLeftEdge = hologramRect.position.x - hologramRect.rect.width / 2f;

            if (hologramLeftEdge <= panelRightEdge)
            {
                Destroy(slotHologram);
            }
            else
            {
                GameObject newThing = (GameObject) Instantiate(hologramSlot.prefab);
                newThing.transform.SetParent(null);
                newThing.transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                newThing.transform.position = new Vector3(newThing.transform.position.x, newThing.transform.position.y, 0f);
            }


            hologramSlot = null;
            slotHologram = null;
        }
    }

    private void SetHologramTransparency(GameObject hologram, float alpha)
    {
        Image image = hologram.GetComponent<Image>();
        if (image != null)
        {
            Color color = image.color;
            color.a = alpha;
            image.color = color;
        }
    }
}

I tried locating the issue but nothing seemed to happen. It is meant to make a transparent object when I click on one of the slots that follows the mouse until it is released. Then if it is on the panel it should disappear and do nothing, but otherwise it should disappear but instantiate a prefab.

Everything else works though it is just I can't see the hologram.

Share Improve this question edited Jan 15 at 7:54 derHugo 91.4k9 gold badges91 silver badges135 bronze badges asked Jan 15 at 6:01 DuckquyDuckquy 131 silver badge4 bronze badges 7
  • 2 what is this line supposed to do exactly? rect.position = new Vector3(rect.position.x, rect.position.y, 0f); .. in general debug the position in your scene/hierarchy .. is the Image a child of a Canvas? – derHugo Commented Jan 15 at 7:51
  • In general I would remove checks like if (image != null) .. it might silently fail and just do nothing .. I wouldn't silently ignore/avoid an exception but rather prefer to get one thrown so I can fix the actual issue .. you could rather make the slot directly of type Image to ensure that the referenced object actually has one and to skip the GetComponent as Instantiate directly returns the type you pass in as prefab – derHugo Commented Jan 15 at 7:53
  • @derHugo that line is meant to make the z = 0; and also the image is a child of the Canvas. – Duckquy Commented Jan 16 at 6:07
  • but I have fixed it sorta by just making a new image like: slotHologram = (GameObject) Instantiate(new GameObject()); slotHologram.AddComponent<Image>(); slotHologram.layer = LayerMask.NameToLayer("UI"); Image image = slotHologram.GetComponent<Image>(); image.sprite = slot.slot.GetComponent<Image>().sprite; – Duckquy Commented Jan 16 at 6:08
  • 1 @derHugo the image is still a child of the canvas, the script is in the canvas – Duckquy Commented Jan 16 at 9:51
 |  Show 2 more comments

1 Answer 1

Reset to default 0

Yeah I have experienced this issue as well, a simple fix is to download the image as a PNG and turn it into a game object and just use code to turn the GameObject on and of again, try adding this code to the asset of the image.

using UnityEngine;



public class Example : MonoBehavior {
   public GameObject Image;
   public bool isShown;

   void Start() {
     isShown = false();
   }

   void Update() {
      if (isShown == true) {
         Image.SetActive(true);
      } else {
        Image.SetActive(false);
      }
   }
}

you will have to reference this script in a different script to use it

转载请注明原文地址:http://anycun.com/QandA/1745597452a90971.html