- Home /
How do I asign this script to a button successfully?
I have a script on a card gameobject that works perfectly if i place an event trigger to click on the card, it will then randomize it's heirachy amongst other cards in the deck area. I want to now shift this function onto a UI button, so when i click the button it perform the action when the card is in the deck area.
this is the current script on the gameobject:
public class ShuffleDeck : MonoBehaviour
{
GameObject Deck;
public bool isShuffleable = false;
int indexNumber;
private void Start()
{
Deck = GameObject.Find("Deck");
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.name == "Deck")
{
isShuffleable = true;
}
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.gameObject.name == "Deck")
{
isShuffleable = false;
}
}
public void Shuffle()
{
indexNumber = Random.Range(0, 10);
if (isShuffleable)
{
transform.SetSiblingIndex(indexNumber);
}
}
}
Thanks in advance :-)
Answer by chiran_janitha · Apr 13, 2020 at 04:51 AM
Drag your object into onclick area in the editor and then you can select Shuffle function from there Make sure function is public.
Thanks for your response. I've tried that and it doesn't seem to function for some reason. Is it because the script is a component of the non-button object?
Your answer
Follow this Question
Related Questions
Highlighted buttons stay highlighted when I deactivate a canvas. No way to unhighlight them. 1 Answer
How to simulate GetKeyDown for a UI button 2 Answers
Clicking a button will display that buttons text in a seperate text-field 1 Answer
Problem with UI buttons and MultiTouch 4 Answers
Creating a Tooltip when hovering over a UI Button? 3 Answers