Clicking anywhere triggers the button - c#

In my scene, I have this set-up:
Game Controller
Level Controller
Main Camera
Event System
Canvas
Player
PlayerCanvas (Render Mode is World Space)
Button
I set the PlayerCanvas into the same width and height of the button. And both of them are just small. I put Debug.Log to check everytime I press the button. But somehow, it triggers the button even if I click way off the screen. Can help to explain why is this happening. Thanks!
Note: I'm trying to add a button beside the Player so that even if the Player moves, the button will just follow.

I think your hierarchy is wrong.
Try something like
Game Controller
Level Controller
Main Camera
Player
Event System
Canvas (Screen Space - Overlay)
Button
Not sure what Player is, but I'm sure on my hierarchy Event System and Canvas are on the same level. And every button is son of Canvas. Not sure you can make multiple Cavas (you should not need it anyway).

Related

Interacting with objects in SteamVR

I'm trying to get Interaction setup in my scene for a colour memory puzzle game in Unity.
I've managed to get the scene changing when the player interacts with a button on a Canvas, so I'm trying to somehow replicate that through a puzzle.
The player can trigger the colour randomizer to display random colours (out of 4), then the player will need to 'press' the buttons on the panel in the correct order to proceed.
So far, I have a canvas overlapping the buttons so the player can interact with them. Each button has a box collider over the button element, so when the mouse is clicked or the trigger button is pressed on the Oculus controller, it should register a button press. But as soon as the hand comes into contact with the 'collider box' the following NullReferenceException Shows;
NullReferenceException: Object reference not set to an instance of an object.
Valve.VR.SteamVR_Action_Boolean_Source.UpdateValue()
I'm trying to figure out a way to reference each 'box' in code to that it knows its that specific colour being pressed. The code is there to accept keyboard input, just having trouble with the VR element.

Unity Button Animation

Does anyone know a possible way for an animation that makes it so when a button gets clicked text pops up above the button that says [Clicked]. It must fade away after 3 seconds and it continues to go up. (The text will move up.)
Pretend like this is how it would look
Clicked but faded away
Clicked
Clicked
Clicked
Button
(Unity Using C#)
How about using a Prefab for this?
You'd create a Prefab with a root object and a text label as a child. Then you animate the text label upwards and fading out over three seconds using the Unity animation window.
On button click, you instantiate the prefab, make it a child of the button, and position it at the same position as your button AND auto-destroy it after 3 seconds.
Here's a code example, pretending that your prefab name is "myAnimatedText"
GameObject animatedButtonText = Instantiate(Resources.Load("myAnimatedText"), transform);
animatedButtonText.transform.position = transform.position;
Destroy(animatedButtonText, 3.0f);

Animating an image not playing in game scene

I am trying to make an animation of a countdown.
I first made a Gameobject, created a canvas for it, and inside that canvas I created a UI Image, attached an image to it, and finally created an animation with that image.
Everything is working fine except the animation is not showing in game scene but only in normal scene, but when I go to that UI Image component and start the game I can see the image changing which means that its not the animation but something else.
Does anyone have an idea of what's going on?
There are several possibilities. Depending on if the image is showing up at all, you can check if your UI image is anchored correctly (four triangle marks) or pivot position is where you want it (circle mark). Depending on how this is done, if you Game window resizes in play-mode with the display set to Free Aspect, your image may move off the visible screen or, in some cases, get crushed which usually shows up as a red X in the editor. I can see this happening if your Game window is set to Maximize on Play.

NGUI OnDragOver() pass through when colliders overlap

we are using NGUI, some collider has to be overlapped, and we want the ONDragOver() event received by the collider behind, ie should pass through to the underneath collider, Here is a demonstration of the situation:
The bigger one is on top of the smaller ones, and they are in the same hierarchy so that I can drag to move all of them.
I have tested that when object dragged over
void OnDragOver(GameObject that)
{
Debug.Log("you are on drag over workpanel not the brick slot");
//Debug.Log(" I am being draged over by : " + that.name);
}
This log will show to indict that the bigger collider will receive the event.
But actually, I want the smaller one in it to receive the event :-(
I have figured this out, change the z position of the collider want to hide behind to a larger value, so the one with a smaller z will get the event first, this may help with people with similar issue.
This may cause the bigger collider not to get the event when hit on the smaller collider, but, whatever, I think this will be good enough for this situation, if someone has a better solution, let us know ;D
And you also should know that the panel meters too. If you have a button collider that became not clickable after colliders overlapped, try to arrange the button in a new panel and tweak the panel depth.

Unity - change image onClick button (C#)

I make menu for my game (Android, C#) and i have simple problem. I want change image in button when the button is pressed. What is the best way currently? Not some preset way in Unity? Thanks
Use button animations. They can be auto generated. Change the sprite in the button press animation manually after generation.
Cost efficient way would be changing sprite object sprite field by getting the UI Image component in the button object from script.

Categories