Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Renaming MonoBehaviours with No References Break

Discussion in 'Scripting' started by javaexp, Jan 8, 2018.

  1. javaexp

    javaexp

    Joined:
    Oct 13, 2014
    Posts:
    8
    Hi,

    I have searched and answers are quite old on this topic. Looking some solution which is smooth.
    I have script referenced in multiple scenes. I want to rename the script.
    If I click on the script to invoke rename (in windows inside Unity Editor) and rename the script then the references of the script get broken and re-creating those references is time consuming. Screenshots below:
    Before:
    http://prntscr.com/hxnnaj
    After:
    http://prntscr.com/hxnnye

    Regards
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Did you rename the class inside the script, too? :)
     
    starblinky and angelonit like this.
  3. javaexp

    javaexp

    Joined:
    Oct 13, 2014
    Posts:
    8
    Hi methos5K,
    No I hadn't. I though Unity would handle that for me.
    So now I opened script in Mono and changed the class name as "public class ScriptGameOver : MonoBehaviour" and I have got back my missing references in the scene also.

    Would there be something else also which I am supposed to do to complete this rename script task? May be an evil string literal containing the script name which I can find using the "Search->Find in Files" feature of Monodevelop. Anything else to be take care of?
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    If you had a string literal, I suppose you'd have to search or remember. I try to avoid those as much as possible.
    As for anything else you need to do when you rename, I believe just those 2 steps. Renaming the file + the class to match is all that you need :)
     
    javaexp likes this.
  5. javaexp

    javaexp

    Joined:
    Oct 13, 2014
    Posts:
    8
    ok got it.
    And since we are taking about rename task here.
    Does rename of scene names, prefabs, particles and textures handled by Unity without any steps from our side?

    I have tried with rename of my 2D sprites, sound files and some prefabs and it was auto updated everywhere by Unity.

    Since you are more experienced in Unity IDE than me so I just making sure here instead of my experimenting and assumptions.
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Ya, pretty sure everything you mentioned is automatically handled :)

    The scripts only give you the correct name when you first make them. Even if you set the name (left the default) and then changed it after and opened it, I'm pretty sure you'd still see the default name inside the script (and that would mean it's not setup correctly).
    It's just something to remember for scripts :)
     
    javaexp likes this.
  7. javaexp

    javaexp

    Joined:
    Oct 13, 2014
    Posts:
    8
    Thanks a ton.
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You're welcome :) Have fun.