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. Dismiss Notice

Question Add Prefix and Suffix to Game Objects

Discussion in 'Scripting' started by WanAmir, Dec 14, 2022.

  1. WanAmir

    WanAmir

    Joined:
    Jul 17, 2018
    Posts:
    27
    [Edited]
    Hi everyone.
    How do I keep the original name of a game object?

    This is my original name game object.
    Screenshot_1.png

    I have a button to change its name in editor mode. It adds index at the front of the original name based on the list.
    Screenshot_3.png

    But when click the button again it become like this.
    Screenshot_4.png

    This is my code:
    Screenshot_2.png

    So back to my question, how do I add index at the front without change the original name "Abutment Sleeve".
    What I want is like:
    0 | Item 1
    1 | Item 2
    2 | Item 3
    ....

    Thank you in advance :)
     
    Last edited: Dec 14, 2022
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    Well, you're clearly delimiting where your string has changed its name, just check if there's a '|' and remove that part.
     
    Bunny83 likes this.
  3. WanAmir

    WanAmir

    Joined:
    Jul 17, 2018
    Posts:
    27
    I'm not sure what you mean, I'm sorry I didn't explain in details. So what I want to achieve is like this:
    0 | Item 1
    1 | Item 2
    2 | Item 3

    but I get:
    0 | 0 | 0 | Item 1
    1 | 1 | Item 2
    2 | Item 3
     
  4. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    525
    It sounds and looks like you are clicking more than once. Each time you click it is looping over the list and changing each item right? So it keeps appending the same value on earlier items. So either change the item you click on or check whether it has been changed already. Mr. Panda is simply saying you can check for the pipe char and remove it (if it exists) before you change it. Me? I would check if it has been changed and simply skip that one.

    I hope the list is long and warrants writing code to modify the names :)
     
    WanAmir likes this.
  5. WanAmir

    WanAmir

    Joined:
    Jul 17, 2018
    Posts:
    27
    Yup, it supposed to click more than once. There are 20-30 items in the list on average. So everytime I click the button (to auto populate the list) it auto changes the item name according to index in the list.
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,895
    Well unless you record the original names of the game objects, you're going to have to parse the string and make sure you don't double up on your formatting.
     
  7. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    Quick and dirty you can check if the string contains '|' and skip the renaming if it does. A more solid option would involve using regex.