Heads up - I'm not very experienced with Unity, and haven't coded in C# before. (I have some experience in Python)

I'm trying to create an app that requires small packets to be sent from a smartphone to a Raspberry Pi. In order to do this, I'm using OSC - or Open Sound Control. I have the Raspberry Pi setup to listen for messages, just having trouble with Unity.

I'm using UnityOSC (there's two of them) Github: https://github.com/thomasfredericks/UnityOSC Documentation: http://thomasfredericks.github.io/UnityOSC/

I'm trying to send a packet of data whenever a button is pressed (and each button needs to send a different packet to identify which button has been pressed). I don't know how I can create a script that is activated with a button press.

Is there a way to make one script with a function that takes the button name as a parameter and then send that?

Thanks for the help, and if you need more information, please ask! Again - I don't have experience with this and I would appreciate a "lower experience level" explination. Thanks!

  • \$\begingroup\$ Yes, the visuals are done - just trying to run a script when they are pressed \$\endgroup\$
    – user100539
    Sep 30, 2017 at 22:27

1 Answer 1

  1. Create a script with a public void method which does what you want.
  2. Put that script on a game object in your scene which is always present. This can be the button itself, if you want, but can also be any other game object which is part of the scene from the start.
  3. Select the button
  4. Click the plus-button in the On Click () panel of the Button's inspector (image 1)
  5. Drag the game object with the script onto the slot which reads None (Object) (image 2)
  6. Select the method from your script (image 3)

Image 1:

image 1

Image 2:

image 2

Image 3:

image 3

  • \$\begingroup\$ Is there a way to use the button name as an input to the function? \$\endgroup\$
    – user100539
    Sep 30, 2017 at 23:12
  • \$\begingroup\$ @CMalasadas AFAIK not directly. But when you put the game object onto the button itself, you can get the name of the current game object with gameObject.name. But usually I would rather create a separate method for each button. \$\endgroup\$
    – Philipp
    Sep 30, 2017 at 23:14
  • \$\begingroup\$ I'm having trouble calling up my public void function - I can't find it listed in unity under the function dropdown. \$\endgroup\$
    – user100539
    Sep 30, 2017 at 23:47

You must log in to answer this question.