Is it possible to get the name of the current script in string format?
I saw that by overriding the ToString method it automaticly returns the name. But is there another way to get the name, so that I dont have to write a ToString method in every script?
ToString() is a public function on every Component. You need it so that it understands that you want a string name back and not something else, like the Component.
The name of the current script in this example is PlayerController and the script is attached to the GameObject Player
Not sure if that is exactly what you wanted, but it was a fun exercise for me!
Thanks! It is almost what I wanted What I am trying to do is to print only the name of the script which in this case is the PlayerController. But having the name in the brackets, I can extract it.
this.GetType().Name will be the fastest method. Alternatively you may use MonoScript.FromMonoBehaviour(this).Name. Slower, allocating and editor only. The first one will give you the name of the class the second the name of the file but both need to be the same in unity. Parsing the this.ToString() result is something you definitely should not do.