- Home /
Passing two GameObjects to a C# onClick script
Hi guys. I'm doing a simple point and click adventure game.
As an example, at some points you will have a choice between a few different options.
What I'd like to do is, when you press a button for one of the choices, to make the button disappear, a textbox appear and some text to be printed. I can do everything I want, but the way is very inelegant. I've stuck 3 onClick scripts on the buttons. One saves the button GameObject in a class variable, the second saves the textbox GameObject and the third fires the function that prints the text with the text ID.
Is there any way I can put more than an argument in an onClick handler, so I don't have to save everything, possibly suffering with race conditions later on?
Here's the relative code so you can see what I'm doing: http://pastebin.com/a23Du31g
Hi,
I don't get it right... In your code there is no OnClick function. And if you use three scripts with OnClick you can put this code together in one function. It would be helpfull, for a better understanding, if you could post the real code and maybe a screenshot how the scripts are attached to the button.
Here's a screenshot of the relevant part. Sorry for the delay. http://i.imgur.com/AQa8vLj.jpg
Restating my problem, see on the right how there are 3 scripts with 1 argument each? I couldn't find a way to put multiple arguments to it.
I could find the button and textbox dynamically, but I feel my way is more future proof.
Answer by hulahoolgames · Jan 10, 2016 at 06:34 PM
What I usually do is create a UI Panel for each main UI screen and put the elements of that UI inside this panel. So you can create a Panel if you wish and put all the buttons and text in this panel. Now on the panel you can add a script, or you can add it anywhere you like (if you dont want to create a panel):
public GameObject button;
public GameObject text;
public void OnButtonClick() {
button.SetActive(false);
text.SetActive(true);
text.GetComponent<Text>().text = "Hello World!";
}
Now add this function as the onclick to the button and attach the button and text gameobjects to the variables. Hope this helps!
Your answer
Follow this Question
Related Questions
Moving GameObjects on elliptical way after clicking on them 1 Answer
Multiple Cars not working 1 Answer
Button On Click with object from different scene 0 Answers
Distribute terrain in zones 3 Answers