get and set function

what is the use of get() and set() function in unity

Properties are not a Unity-specific topic. Where did you encounter them?

Get and Set are parts of C#. They are used to hide variables. This is useless code that says if you want to use `_val`, you have to call it `val` instead:

int _val;
val { get { return _val; } set { _val = value; } }

Some inexperienced C# programmers think you need them. I've seen some Unity snippets using get/set where you could tell the programmer was following a book and is still learning C#. If you see longer get/sets with some ifs in them (for example) it probably means an experienced programmer writing solid, but not beginner-friendly code.

they are for normal using on Programming object oriented also you can make the object do something when the get or set are being called

string _life;
      //Do something else
      return _life;
      if (value <= 0)
         _life = value;
         //keep doing things here or before

hope this helps