Search Unity

How to permanently change the name of objects in hierarchy by script?

Discussion in 'Scripting' started by eskovas, Mar 1, 2011.

  1. eskovas

    eskovas

    Joined:
    Dec 2, 2009
    Posts:
    1,373
    hi all,
    well i've been trying to find the awnser, but didn´t find it... so i'm posting here, well:

    what i want is to change the name of an object in the hierarchy,
    for example, i have many cubes with the same name, and i need to change every single cube's name to another distinctive name and save it, so after i end the play session i have every cube with different names.
    i only need the code to change one,
    is there any function that lets me do this?

    thanks in advance :)
     
    Last edited: Mar 1, 2011
  2. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    You'll probably need to use an editor script for this. For some examples, check out the 'editor scripts' section of the script wiki. (Sorry, don't have a link handy at the moment.)
     
  3. eskovas

    eskovas

    Joined:
    Dec 2, 2009
    Posts:
    1,373
    anyone?
    i alrealdy searched everything in the wiki section and in the unity script reference and didn't find anything, and what i found doesn't work...
     
  4. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    I don't know that you're going to find a script that does exactly what you're wanting. You might, but it's more likely you'll need to write it yourself. (If you're running into problems with that you can always post your code here along with a description of the problem, and someone should be able to help.)
     
  5. eskovas

    eskovas

    Joined:
    Dec 2, 2009
    Posts:
    1,373
    the "problem" that i'm having is that when i end the play session, all names come back to the original... (i know why it happens)
    i know there isn't a script, what i'm looking for is a function that changes the name of the object permanently, so i dont have to change every single one manually, the rest i know what and how to do xD
     
  6. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    To change the name of a game object, you can simply assign a new value to the 'name' property, e.g.:

    Code (csharp):
    1. go.name = "name";
    It's been a while since I've done any editor scripting, but if I'm not mistaken, if this change is made via an editor script while in edit mode (as opposed to play mode), the change should be 'permanent' (provided it's saved with the scene at some point).

    A reasonable way to invoke this code would probably be via a menu command, which the 'extending the editor' portion of the documentation or the script wiki should show you how to do. From there, you'll probably want to find all the objects of interest by name, tag, type, or selection (whichever is appropriate), and then assign new names as desired.

    Again, it's been a while since I've worked with the editor in this way, but I think the above will work. (If not though, perhaps someone else will provide an alternate solution.)
     
  7. eskovas

    eskovas

    Joined:
    Dec 2, 2009
    Posts:
    1,373
    thanks,
    i'll try that
    i'll let you know if it works
     
  8. eskovas

    eskovas

    Joined:
    Dec 2, 2009
    Posts:
    1,373
    just wanted to say that it's resolved,
    thanks for your help :)
    here's what i needed to do : ( changing every children's name in an object)

    Code (csharp):
    1.  
    2. @script ExecuteInEditMode()
    3.  
    4. function Start () {
    5.  
    6.     for (var Children : Transform in transform){
    7.        
    8.         Children.name = Children.name + Random.Range ( -10.000, 10.000);
    9.  
    10.     }
    11. }
    12.  
    really simple

    soon i'll reveal what i am going to release on the asset store :D i think it's going to be very usefull to many people