I am using image (Amazon Icon) as button with Event Trigger Pointer Click component but it's not working when I click.Sometimes it's working while clicking one specific place at icon but I don't know what is triggering it.Is there any way to make it work? I am not able to use button because of my canvas is small and it's being blurry when I use buttons.
pcinteract script:
`
public void start(){
number++;
Debug.Log("Clicked:" + number);
}
Screenshot from game screen
Inspector panel of Amazon Icon
working area
It appears that there may be an issue with the event trigger component on the Amazon icon game object. To troubleshoot this, you can try the following steps:
Verify the presence of a collider component on the Amazon icon game object. If it is not present, add it so that clicks can be detected.
Ensure that the "Is Trigger" option within the collider component has been checked.
Scrutinize the order of components in the event trigger component, making certain that the "Pointer Click" component is above the others.
Consider incorporating a Debug.Log() statement into the start method, to determine if the method is being
called upon clicking the Amazon icon.
If these steps prove ineffective, you may want to experiment with creating a new game object and adding an image component, collider component, and event trigger component to it. Test clicking the new object to see if it functions as intended.
Related
I want to create a button that has a custom shape in Unity.
I created a default UI>Button object and set it's Image to the following sprite:
It doesn't show it on this website, but the circle is the only part of this image that is transparent. It's a .png sprite.
When I set this as the sprite for my button, I could click on the entire rect transform of this sprite to activate the button using my button's OnPointerDown() method and un-hide some other sprites around it:
public override void OnPointerDown(PointerEventData eventData)
{
base.OnPointerDown(eventData);
Debug.Log("Foo");
}
Each time any part of the sprite was clicked, transparent or not, "Foo" would print.
Now I set the button alphaHitTestMinimumThreshold to 0.5:
GetComponent<Button>().image.alphaHitTestMinimumThreshold = .5f;
And as the docs suggest, I disabled atlassing...
...and enabled read/write for the sprite:
Yet now, when I click the button in my scene.. nothing happens. No matter where I click, "Foo" doesn't print anymore.
Then I tried changing the alphaHitTestMinimumThreshold to a lot of different values. Again, the entire button was not clickable anymore. If I don't touch the alphaHitTestMinimumTreshold at all however, my "Foo" prints again.
Why does changing alphaHitTestMinimumTreshold to any value make my OnPointerDown method to never be called?
So after 12 hours of messing around with all sorts of options, I found out that there were 3+ consecutive issues causing this behaviour.
If you ever run into your transparent buttons not working, be sure to try the following steps:
Make sure read/write is enabled in the texture import settings
Try if setting the Sprite Mode Mesh Type to Full Rect helps (sometimes the button's hitbox is off)
Disable atlassing under Project Settings > Editor > Sprite Packer > Mode
..and finally what did the trick for me: if you're working in a prefab that's nested inside another prefab (say your have a prefab for your button and then another prefab for your UI canvas/contents), remove the nested prefab from the parent prefab (aka remove the button prefab from the UI prefab), and just add it back in again. It was that freaking simple for me.
I triple checked whether my nested button prefab differed from the button prefab blueprint, and it didn't. But somehow, removing it from the UI prefab and adding it back in again solved my problem with a large sprinkle of magic.
With transparency buttons, I found that detection improves when the image is a child of the button object. So hierarchy is Canvas>Button>Image.
I know, old thread, but it came up so heres my solution:
It did all of the suggestions, but it didn't work.
HOWEVER,
What finally worked for me, was TO SPECIFY THE RIGHT SIZE. My game Object was elongated, but the sprite had "preserve aspect ratio" ticked. I just didn't bother to size it properly.
So when you have an image, set the size of the gameObject to the right dimensions.
I am new to unity and I am still learning. I am making a game and its going great but I have a problem with a Quit button.
It doesn't use the script attached to the On Click () section in the button menu.
There's an EventSystem in the Hierarchy, the Force Module Active is checked, the button is a child to a canvas. I don't know why the button isn't working
Here is the code in the script attached to On Click ()
public void Quit()
{
Debug.Log ("YES IT WORKS");
Application.Quit ();
}
I want the program to close when the button is pressed but when the button is pressed nothing happens!
The function is not being called when the button is pressed which means that you have not selected the function in the onClick settings. To do this, ensure that the script is attached to the button and then dragged into the onClick box of the button in the inspector. Next, click on the dropdown and hover over the name of the attached script and select the function that you want to call.
Solution
Instead of using Application.Quit() try to shift the your working scene to the home screen .Your every game should start and end at the same level which is also known as the main scene of the the game. Secondly before shifting to other scene you should destroy your player and save the scores in the player pref .
Player Pref:
The library which helps the user to store the data and show them on the main screen , Hence the use of database will not be there in order to save the data for the small games . You are beginner so you should look at these all thing on You tube or the unity website documentation available on its website
Are you in the Editor?
Application.Quit() only works when you build the application (for PC and Android. It should not work on iOS, and even if it works it should display as a crash so it's not good).
I've made a simple animation for my dishwashing machine in 3dsmax. I imported the object as an fbx file into Unity. The animation is called "openandclose". The animation itself opens and closes the machine.
However I can't seem to find a way to trigger the animation to happen once at the click of a button. How can I do this?
Unity has a great documentation regarding every core function including animations. Check this link out: http://docs.unity3d.com/462/Documentation/ScriptReference/Animation.Play.html
and to get the button pressed:
http://docs.unity3d.com/462/Documentation/ScriptReference/Input.GetKeyDown.html
combined it would be something like this i guess:
void Update() {
if (Input.GetKeyDown("e"))
animation.Play("openandclose");
}
I have a problem cancelling pan feature of my app when a button in HUD is pressed.
We're using canvas for displaying UIs, so I can't use the RayCast to detect if a touch hit a button or not since the button is not directly in the world where the map to be panned located.
I already added collider to the buttons and tried printing the name of objects hitted by raycast but it just passes through the buttons like they are not there.
How am I suppose to detect if a button in canvas is touched so I can cancel executing pan feature?
Check EventSystem's IsPointerOverGameObject method. You can use it like this:
public bool IsPointerOverUI
{
get{
return EventSystem.current.IsPointerOverGameObject();
}
}
Don't forget to import UnityEngine.EventSystems
I am designing a shooting game in Unity 3d for which I have a requirement that only when an image is tapped the shooting should occur. Please check the command used for the purpose:
if (Input.GetMouseButtonDown(0)){
if (!clickedConfirmed){
GameObjectClicked = GetClickedGameObject();
clickedConfirmed = true;
}
Currently, shooting occurs when clicked anywhere on the screen. How can activation of shooting be bonded only to the gameobject (image) instead of being activated when clicked anywhere on the screen.
Your need to give more context in your example, especially what GetClickedGameObject() is actually doing internally.
As such I'm limited in what I can say is the problem, but I suspect that you aren't using GameObjectClicked correctly.
I would expect that you'd do a check such as:
this.gameObject == GameObjectClicked
Note that if this being used as a method of developing a UI there are more elegant solutions out there, including the long awaited new GUI system included in the Unity3d 4.6 open beta.