Add Prefix and Suffix to Game Objects

[Edited]
Hi everyone.
How do I keep the original name of a game object?

This is my original name game object.
8660409--1166094--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.
8660409--1166097--Screenshot_3.png

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

This is my code:
8660409--1166115--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 :slight_smile:

Well, you’re clearly delimiting where your string has changed its name, just check if there’s a ‘|’ and remove that part.

1 Like

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

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 :slight_smile:

1 Like

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.

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.

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.