Answer the question
In order to leave comments, you need to log in
I don't understand what is the problem?
TO THE ESSENCE-I am a beginner programmer, I am trying to make a strategy according to the tutorial on YouTube, here is the script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BuildManager : MonoBehaviour
void Start() {
}
void Update() {
}
void OnMouseEnter()
{
transform.GetChild(0).GetComponent<Image>().color = Color.green;
}
void OnMouseExit()
{
transform.GetChild(0).GetComponent<Image>().color = Color.white;
}
}
Answer the question
In order to leave comments, you need to log in
That's how it works. Checked.
Instead of the Image type, it's better to specify the Graphic type if this script will only change the color. Well, in this case, name the variable _graphics, not _image.
using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace Assets.Scripts
{
public class BuildManager : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
private Image _image;
void Start()
{
_image = GetComponent<Image>();
if (_image is null)
{
throw new NullReferenceException(
$"The component of the type \"{nameof(Image)}\" not found.");
}
}
void Update()
{
}
public void OnPointerEnter(PointerEventData eventData)
{
_image.color = Color.green;
}
public void OnPointerExit(PointerEventData eventData)
{
_image.color = Color.white;
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question