How do I fix this?
This works, so you must be missing something. Did you set up the OnClick Object reference correctly? Is the OnClick object reference linked to an existing GameObject or are you referencing the script directly? Typos or scripting errors maybe?