- Home /
changing script from key based function to GUI button for dice roll game
I am making a simple roll a dice game, for which i have taken help from tutorial regarding the scripts. According to the script basically dice rolling script, it first roll the dice at the start of game and one have to press the spacebar key to again roll the dice. But i want to use the button instead of key for rolling the dice. That means player will have to press the button in an order to roll a die. I have tried to change the script but was unable to get the result. Any suggestions are highly acceptable. :)
Answer by Piyush_Pandey · Jul 18, 2018 at 10:36 AM
1) Add a UI button your scene/prefab.
2) Create a new method eg "RollDIce"
3) Transit the code you are having in your Update method on space bar key Input to this method.
4) Remove the Update method
class DiceScript:MonoBehaviour
{
void RollDice()
{
//the code of player pressing the Space bar from update
}
}
5)Now, assign the button action this RollDIce method from the inspector.
Thanks for your reply..But the situation is that i dont want to use spacebar or any other key..i want that when button is pressed then dice is rolled..and i tried your method too but it didnt worked as expected..
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
OnClick() animation 0 Answers
Add force when button is pressed 2 Answers
Making a ship control panel 0 Answers
Distribute terrain in zones 3 Answers