I am using DOTween so that when I hover over the button it highlights by enlarging slightly and tweens into it to look smooth. However, I have two separate scripts attached for each button which do exactly the same thing but when I hover over one of the buttons the other button also enlarges. I've tried using the buttons under the same canvas which didn't work. So then I tried using two canvases for separate buttons and it still the same issue. Looks like this: https://vimeo.com/user105553995/review/517200220/41fbe8d619
My code is in different scripts for each button but exactly the same:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using DG.Tweening;
public class OtherButton : MonoBehaviour
{
// Start is called before the first frame update
private Vector3 defaultScale;
// Start is called before the first frame update
void Start()
{
defaultScale = this.transform.localScale;
}
// Update is called once per frame
void Update()
{
WhilePointerHover();
}
private void WhilePointerHover()
{
if (EventSystem.current.IsPointerOverGameObject())
{
Vector3 enlarge = defaultScale*1.2f;
this.transform.DOScale(enlarge, 0.05f);
Debug.Log(this.gameObject.name);
}
else
this.transform.localScale = defaultScale;
}
}
Any help is appreciated!
Code checks if you are over any UI object, so any script will react.
You should use https://docs.unity3d.com/2019.1/Documentation/ScriptReference/UI.Selectable.OnPointerEnter.html
And
https://docs.unity3d.com/2019.1/Documentation/ScriptReference/UI.Selectable.OnPointerExit.html
So it will only react to the object to which it is attached to. Note that the Image or Button component needs to be on same object.