Hover over one button affects other button as well - c#

I have two buttons like so:
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.

Related

enable/disable Objects in Unity

I am totally new in programmation and unity, so I have hard time with basically everything!
Here is my issue : I have a 2D static game with a grid of boxes. each box is made of buttons to click.
I want all the boxes but one not visible at the beginning, and then the box have a button to make boxes appears one by one.
here is my code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OpenBox : MonoBehaviour
{
// Start is called before the first frame update
private GameObject boite1;
void Start()
{
box1 = GetComponent<Box1> ();
}
void Update()
{
if (Input.GetKeyUp(KeyCode.Space))
{
box1.enabled = true;
}
}
}
The "Box1" is underline in red with message : CS0246, The type or namespace name could not be found.
I am not sure I know how to refer to the game object.
thank you for your help !
try use
gameObject.SetActive(false);
more detail unity doc

Empty GameObject not deactivating

everyone.
Basically, I have a prefab with a canvas and an empty GameObject. This GameObject, PlayAd, contains its own canvas and a button to play ads and let the users skip a level. PlayAd's button is covering the other canvas's continue button, which is only supposed to be accessible if the user passes a level. All my script is supposed to do is deactivate PlayAd on trigger enter so the users can get to the continue button. However, it never goes away after I reach the trigger to cause this. Any help on this would be appreciated. Thank you.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class NewLevScript : MonoBehaviour
{
public GameObject PlayAd;
// Start is called before the first frame update
private void Start()
{
PlayAd = GameObject.Find("PlayAd");
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Platyer")
{
PlayAd.SetActive(false);
}
}
}
Boy, do I feel dumb... I had to remove the "t". It works now.

Unity3d C# - How to check if a button has been pressed, and then instantiate an object

So for the past two days (no joke) I have been trying to figure out how to check if a button is pressed, and then if so, make it instantiate an object. I have tried multiple methods so far and maybe one of those methods were closer to getting where I wanted but for now I will just show what I have currently. Right now, the problem is me detecting if the button has even been clicked in the first place.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class Master : MonoBehaviour
{
public Button storeButton;
public Transform hands;
public GameObject gun1;
void OnEnable()
{
gun1 = GameObject.FindGameObjectWithTag("Weapon1");
hands = GameObject.FindGameObjectWithTag("Player").transform;
// storeButton.onClick.AddListener(MyFunction);//adds a listener for when you click the button
storeButton.onClick.AddListener(() => MyFunction());
}
void MyFunction()
{
Debug.Log("YOUR PRESSED THE DAMN BUTTON");
// Instantiate(gun1, hands.position, hands.rotation);
GameObject.Instantiate(gun1, hands.position, hands.rotation);
}
}
Add an EventSystem to your Scene.
Otherwise you cant click Buttons an other Stuff.
http://docs.unity3d.com/Manual/EventSystem.html

Hiding a UI panel from script in unity

I have a UI Panel containing a Button as a child. The button has a script as a component. The script is as follows:
public GameObject Panel ;
OnMouseDown()
{
Panel.setActive(false);
StartCoroutine(takeShot());
}
private IEnumerator takeShot()
{
Application.CaptureScreenshot("my_img.png");
}
I am having a problem saying the coroutine can't start because the button is inactive.
How can I fix this problem? Can I hide the panel without using SetActive(false)?
Yes I assume you are using the latest version of the unity UI.
You'd want to make use of canvas groups. Attach a canvas group to your parent object, the panel, in your code access the canvas group component and set its alpha to 0 on whatever your trigger may be. This hides the canvas element and all its children but is still active in the scene. Reset it to 1 to make it visible again. Unfortunately you will still be able to interact with it as it is still technically in the scene, so you can solve this by using Renderer.enabled and setting it too false. This updates it but doesn't draw it. SetActive stops it altogether.
Hope this clears things up.
I have been battling with the same problem for a while. However, i adopted a different answer that has the same effect. What i did was access the RectTransform component and then hide it by scaling it down.
public RectTransform Panel;
void Update ()
{
if(Input.GetButtonUp("Fire1"))
{
Panel.localScale = new Vector3(1, 1);
}
if(Input.GetButtonUp("Fire2"))
{
Panel.localScale = new Vector3(0, 0);
}
}
For hiding any component in Unity3D without using SetActive() method, you can set the scale of the object as zero.
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class HidePanelDemo: MonoBehaviour
{
public RectTransform panelObject;
// Start is called before the first frame update
void Start() {
panelObject.localScale = new Vector3(0,0,0);
}
}

Find a GameObject and check if the user has clicked on it

I have recently been making the UI for a game I am creating and have run into a problem.
I have been trying to find a way in the new Unity 4.6 for the user to be able to click on a player card and have it select the player they clicked on.
public void Panel1Click()
{
GameManager.Player1Select ();
}
This is the way I am doing it at the moment, calling this when the player clicks on Panel 1, there are also 3 more for each of them.
I have been researching different methods on how to find the object the player clicks the execute the correct selecting code.
if (GameObject.Find ("Panel 1"))
{
print ("Click Panel 1");
GameManager.Player1Select();
}
This is one of the methods I tried, however nothing gets called. (Because it just checks if the object exists/is true? I think).
All these methods are linked to the EventSystem component on the panels.
Is there a more efficient way of condensing all the functions and just checking which panel the player clicks on?
You can add a collider to the game object (sprite), and then in the OnMouseDown function to test if it is clicked.
Select the card --> Add a box collider to it --> Add a MonoBehaviour script and attach to the card --> In the script, add function:
bool bClicked = false;
void OnMouseDown()
{
Debug.Log(gameObject.name + " is clicked");
bClicked = true;
}
You can actually have one parameter for your click handler. Supported types are: int, float, string and object reference. So you can define your handler like this:
public void SelectCharacter(int character) {
GameManager.PlayerSelect(character)
}
Then just set the parameter in the event trigger.
Create a script and put it on the object(Panel) you want to interact with, then try this code. In this example it finds the parent panel and sets it to inactive
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class CloseInventory : MonoBehaviour, IPointerDownHandler
{
GameObject inventoryPanel;
// Use this for initialization
void Start()
{
inventoryPanel = GameObject.Find("Inventory Panel");
}
public void OnPointerDown(PointerEventData eventData)
{
//SET WHAT TO DO HERE
inventoryPanel.SetActive(false);
}
}

Categories