I have a function where I instantiate buttons and add them a method that is supposed to execute on click.
All this works fine, but when I click for the first time on a button, if I don’t click somewhere else after that, every time I will instantiate new buttons will also apply my OnClick method… (which is not what I’m trying to do)
Here is a sample of my code, if you have an idea about what I’m doing wrong here ?
Oh, and also, could you tell me what’s the difference between those 2 lines ? In my code it works exactly the same way but maybe it’s not always the case ?
I’m not sure exactly what this means. Could you post the OnClickMethod code and describe exactly what happens and what you expect to happen?
The second part is understanding what the compilers sees:
Lets assume you have this function:
bool IsValid()
{
return true;
}
When you type
IsValid()
// or
bool SomeBool = IsValid();
The compiler sees IsValid() as a bool since thats its return type.
If you type this:
button.onClick.AddListener(IsValid());
The compiler sees you but a bool in there will complain. What it wants is a reference to that method. Which when you type IsValid without the () is the reference to that method. You can can get that reference in two ways:
delegate is a keyword to let the compiler now the following method so it returns a reference to that method.
Now the () => is s lambda expression It basically is a way to create a method on the fly. It returns a delegate as its return value:
So if you could do this:
// This is now a type like a int or a float
delegate bool MyDel(int x, int y);
void Start() {
int a = 5;
int b = 6;
MyDel SomeCheck = (x, y) => { if (x < y) return true; else return false; }
if (SomeCheck(a,b))
// do stuff
}
But this also returns a delegate:
() => OnClickMethod();
Which is why it works the same as explictly using the delegate keyword.
I think that’s unrelated to your code; if you have a button focused and you press Enter, it clicks the button. If you want to stop that, you need to go to the Input settings for your project and change the “Submit” input to something other than Enter. Or it might be better to change your own code to not use Enter as the button-spawning key.
Not really a big difference, though both ways make it harder to remove the listener later if you needed to. The most correct way to do it, and also the shortest, is like this:
Good Catch on the fact he’s using enter to Instantiate new things and also the default button press.
Another easy thing to do is Change your Instantiate code to be a different KeyPress than Enter
Really depends on what you are doing. For me I seldom reuse buttons, so removing listeners isn’t a big deal.
I like the lambda syntax because it means I don’t have to define a new method for every variation on a button. Lambda also lets you ‘bake’ variables into the function call.
Of course the OP is not doing any of that. So using the method directly works just as well.
Yeah, it’s just in this case the method is already there and the OP didn’t seem aware that there was a shorter way to do it which also has the added benefit of letting you unsubscribe from the event. Lambdas are certainly useful when you need them.
Sorry for answering late, I got a little problem on my project (lost the latest version ), but makeshiftwings found the reason of my previous problem, with the “return” input that is the default submit input !
Thanks for that, I could have struggled with it for quite some time !
Now I’ll go watch some tuto about delegate cause I don’t know enough about it to use it properly but it seems very powerful !
Thanks to all of you for your answers && have a nice day !