- Home /
unity onlick function not showing
I have this code for a rock paper scissor game, its just the beginning, I'm trying to attach an onlick to my object but i cannot find the function in the scroll..
here is my code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Gamemanager : MonoBehaviour { enum elements { Scissor , Paper , Rock}
private int playerChoose = -1;
private int botChoose = -1;
private bool playersTurn = true;
// Update is called once per frame
void Update()
{
if (playersTurn && playerChoose == -1) return;
}
public void playerChoose (int choose)
{
playerChoose = choose;
}
}
I would really appreciate some help.. thank you
Answer by JonPQ · Feb 14, 2019 at 04:42 PM
in the button / onClick component, hit the '+' then drag in the gameobject with your script on it to that slot, then you should see a dropdown with all the call-able functions from any scripts on that gameObject. If you still dont see your function try making it public.
Thanks for your response. When I drag it the same thing happens because Ive already paired my script to the gameObject, I've also tried making them public and still nothing...
I even deleted the whole unity program and re-downloaded it.
Answer by Von_Dutch · Feb 14, 2019 at 05:21 PM
I think I know. For some reason you can add a function with zero or one parameter.
public void add(); works public void add(int a); works public void add(int a, int b); doesn't show up
I'm thinking a way around that is using the older Ongui functions to handle that, but haven't tested yet.
Answer by Bunny83 · Feb 14, 2019 at 05:55 PM
You have given your variable and your method the same name:
private int playerChoose = -1;
public void playerChoose (int choose)
{
playerChoose = choose;
}
While overloading members is allowed when they differ in their signature, it can cause tons of issue with serialization systems. You generally should avoid this.
Your answer
Follow this Question
Related Questions
Material Disappeared after destroyed 0 Answers
How to trigger a button click from script 3 Answers
How to make On Click() work with prefabs? 1 Answer
How to create a onClick addlistener from text parameter? 2 Answers
How to make a custom onClick event? 2 Answers