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.
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
rect.position = new Vector3(rect.position.x, rect.position.y, 0f);
.. in general debug the position in your scene/hierarchy .. is theImage
a child of aCanvas
? – derHugo Commented Jan 15 at 7:51if (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 theslot
directly of typeImage
to ensure that the referenced object actually has one and to skip theGetComponent
asInstantiate
directly returns the type you pass in as prefab – derHugo Commented Jan 15 at 7:53slotHologram = (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