Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Get name of script

Discussion in 'Scripting' started by Mish, Sep 10, 2015.

Thread Status:
Not open for further replies.
  1. Mish

    Mish

    Joined:
    Apr 23, 2010
    Posts:
    96
    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?
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    this.name


    ... nope, thats the current gameobjects name.. hmmm
     
  3. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,386
    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.
     
  4. roojerry

    roojerry

    Joined:
    Mar 18, 2013
    Posts:
    68
    this.GetType().ToString()

    However, class name as a string is generally not something you should be using. What are you wanting to do with this?
     
    ftejada and Remory52 like this.
  5. Mish

    Mish

    Joined:
    Apr 23, 2010
    Posts:
    96
    It's for a custom editor, I need to show the names of the scripts in a node structure.
     
    julian-moschuering likes this.
  6. holliebuckets

    holliebuckets

    Moderator

    Joined:
    Oct 23, 2014
    Posts:
    496
    I love this thread ^_^ This is what I have:

    Code (CSharp):
    1. Debug.Log (this.gameObject.GetComponent<MonoBehaviour>())
    It Debugs:
    Code (CSharp):
    1. Player (PlayerController)
    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! :D
     
    Last edited: Sep 28, 2015
    hworld likes this.
  7. Mish

    Mish

    Joined:
    Apr 23, 2010
    Posts:
    96
    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.
     
    holliebuckets likes this.
  8. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860


    New around here? We encourage the use of code tags instead of font changes. Makes everything nice and consistent and readable.

    One of your collueges even put up a sticky about it. :)
     
    holliebuckets likes this.
  9. holliebuckets

    holliebuckets

    Moderator

    Joined:
    Oct 23, 2014
    Posts:
    496
    ty! fixed for best practice ^_^
     
    Kiwasi and JoeStrout like this.
  10. julian-moschuering

    julian-moschuering

    Joined:
    Apr 15, 2014
    Posts:
    529
    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.
     
    Mish likes this.
Thread Status:
Not open for further replies.