Unity - change image onClick button (C#) - 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.

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.

Clicking anywhere triggers the button

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).

Unity: Add Button Script to 3D gameObject

I'm trying to add the Unity Button Script to a 3d gameObject. When I tried this by adding it onto a cube, I can't get the desired behaviours. How can I get the 3d object to work like a button? In particular the highlighting colors, the click events. I was speculating that perhaps it uses the wrong raycaster, and if the raycaster specificaiton is changed some how it would work? Does anyone have a good enough understanding of the source code to explain if this is possible, and if so what I would need to do to achieve this? Thanks.
Rik
You could do it by using OnMouseDown() function. Here is the link Unity Docs
You will also need to add the image script along with the button script.

Unity 5.4 Button auto-clicks

When I create a button in unity3d 5.4 the button just clicks automatic when i press play in the editor. I haven't been using unity before, so I can't say if it would happen in other versions.
First i create a script, then I attatch it to a empty gameobject. Then I press the little + sign on the OnClick() in the button properties, so I can add the gameobject with the script attached.
I have screenshots of button properties, eventSystem, Canvas, gameobject and the Script.
If anyone know what i have done wrong, please let me know. Thanks in advance.
The screenshots are in this post, since I cant post 5 images in stackoverflow:
http://forum.unity3d.com/threads/unity-5-4-button-auto-clicks.426526/
When you press play in editor, Button is NOT pressed at all. Its just that you have Debug.Log("Clicked!") in Start() method. Start() is called by Unity automatically when you run the application. It has nothing to do with button click. You need to register OnClick() listener method so it will be called when you press the button.
Have a look at this tutorial : https://unity3d.com/learn/tutorials/topics/user-interface-ui/ui-button
EDIT :
Please learn more about scripting here: https://unity3d.com/learn/tutorials/topics/scripting

unity 5 button not showing up in editor

I'm trying to create a start menu for my unity 5.3.1 game by following this tutorial : https://www.youtube.com/watch?v=pT4uca2bSgc
I followed everything he said up to the part when he attached the script to the start button on the main menu screen. Here is a picture of my code and editor:
Any help is appreciated thanks.
You have attached that Title script to the "canvas" Canvas Game Object - not the "start1" Button Game Object.
In reality in doesn't matter, because you can tell your button to call any method on any game object when clicked.
To do that, select your button, for the OnClick event drag the Canvas game object, then from the drop down select the Title script and the startLevel() method.
Now when you click the button your level will load.

Categories