- Home /
The question is answered, right answer was accepted
How to remove the (Clone) from instantiated prefab C#
I've tried to remove the the (Clone) by renaming the instantiated prefab but it stays! eg: Fighter0*(Clone)*
How can I remove it?
Thanks!
public void Spawner()
{
ClassPick = 0; //(int)Random.Range(0,2.999f);
Debug.Log("Setting Class");
switch(ClassPick)
{
case 0 :
enemy = Resources.Load("Fighter") as GameObject;
Instantiate(enemy, new Vector3(0,0,10),Quaternion.identity);
enemy.name = "Fighter" + prefabIndex;
ecounter++;
break;
}
}
Thanks guys! Just got a message that this was answered.
Answer by GibTreaty · Aug 03, 2012 at 06:44 AM
Use
GameObject go = Instantiate(enemy, new Vector3(0,0,10),Quaternion.identity) as GameObject;
go.name = go.name.Replace("(Clone)", "");
I didn't realize before that he was actually setting the prefab's name and not the instantiated object's name.
It's easier this way:
GameObject go = Instantiate(enemy /* ...*/ );
go.name = enemy.name;
Even easier (if you do not need to keep track of the prefab). Instantiate(prefab).name = prefab.name; This should instantiate the object and change its name to the one in the prefab.
Well, it's not really easier. It's just a bit shorter but more cryptic since you do several things in a single line. This is bad for debugging (exceptions only tell you line numbers. If you do 5 things in a single line you have no idea what part failed). Also in most cases you usually need to do several additional things to the instantiated object or you want to store it in some kind of list to keep track of the object. In both cases you need an explicit local variable.
That variable does not have any impact on the performance.
This works perfectly GibTreaty! Answer should be accepted since it does exactly what is asked here
Answer by Radon · Aug 03, 2012 at 06:40 AM
Your Gameobject's name is not changing. It is the system telling itself in the inspector that the original object is not a clone so you don't accidentally erase it. If you apply your code to a specific parent name or game tag, the script shall work on clones, too.
He's not trying to Destroy the object, he's trying to rename it.