If you had used code tags when you wrote your question, you would have noticed that you are missing bracket from your code blocks. If that is actually your code, your code won’t compile and you should probably also see error in your Unity console.
So why not edit your question to use code tags, here is how:
Also, this question is not specifically about 2D features, you are asking why your code is not working, so you should ask such question in general scripting forum.