B
B
bebra1232021-10-14 17:54:44
Unity
bebra123, 2021-10-14 17:54:44

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;
    }
}

This script, in theory, should highlight it when hovering over a cell, and when you take it away, it should be highlighted.
Please don't hit with slippers.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Boris the Animal, 2021-10-15
@Casper-SC

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 question

Ask a Question

731 491 924 answers to any question