Removing "(Cloned)" from instantiated object name

Javascript
I have an enemy spawning script that instantiates prefabs. When the prefab is instantiated it gets (Clone) after its name, and this is messing up the rest of the game because damage and things like that use the enemies name. So, how can I remove the (Clone) after the enemies name when its instantiated? I feel like it could be simple but I just don’t know where to start, and any examples I have found are in C# and I need Javascript. Here is the code I am using:

var sBlack : Transform;

private var keepSpawning : boolean = true;

function Start () 
{
if (keepSpawning == true)
{
	Spawn();
}

}

function Spawn()
{
	yield WaitForSeconds (2);
	Instantiate (sBlack, transform.position, transform.rotation);
}
function Start()
{
 transform.name = transform.name.Replace("(clone)","").Trim();
}
4 Likes

Isn’t managing objects by name inefficient?

Solution as above
could you also directly set the length of the name to be 7 characters shorter, effectively trimming them off? i forget if length is read only

Probably no real difference to finding by tag, which is the solution most people use.

It really depends on where and how you use find.

Thanks for the suggestion. This is what I have but the name is not changing, I tried putting the line in Update but thats not working either.

var sBlack : Transform;

private var keepSpawning : boolean = true;

function Start () 
{
transform.name = transform.name.Replace("(clone)","Skeleton_Black").Trim();

if (keepSpawning == true)
{
	Spawn();
}

}

function Spawn()
{
	yield WaitForSeconds (2);
	Instantiate (sBlack, transform.position, transform.rotation);
}

The Trim is redundant in your code and defeats the purpose of the original Trim - to remove the space usually at the start of (clone).

I assume your problem is you are setting the name on the wrong GameObjects. The code I provided needs to be run on a script that is attached to the object you want renamed.

As I see you are Instantiating sBlack, I assume you are wanting this to have its name fixed…

function Spawn()
{
   Transform skeleton = (Transform)Instantiate (sBlack, transform.position, transform.rotation);
   skeleton.name = skeleton.name.Replace(" (clone)", "Skeleton_Black");
}

Oh I see what you mean I think. I would like to use the second method, but Im getting an error for a semi-colon, not sure what is exactly wrong. That is javascript right?

var sBlack : Transform;

private var keepSpawning : boolean = true;

function Start () 
{


if (keepSpawning == true)
{
	Spawn();
	
}

}

function Spawn()
{
	yield WaitForSeconds (2);
	Instantiate (sBlack, transform.position, transform.rotation);
	Transform skeleton = (Transform)Instantiate (sBlack, transform.position, transform.rotation);
       skeleton.name = skeleton.name.Replace(" (clone)", "Skeleton_Black");
  1. Get rid of the first instantiate. You dont want two in there, otherwise youll get two skeletons for each Spawn call.
  2. Cant tell if this is all the code, but it looks like you are missing a couple of } } chars at the end

actually, that was c#… try something like this…

var skeleton : Transform = (Transform)Instantiate (sBlack, transform.position, transform.rotation);
skeleton.name = skeleton.name.Replace(" (clone)", "Skeleton_Black");

I dont write JS, so not 100% sure if thats syntax correct

Yea its missing the last }, but that is just because I didnt highlight, its in the actual code. hmm it seems to be something with those two lines because when I comment the code out the error goes away, even after I remove the first Instantiate. The syntax looks funny but I am sort of a beginner so I can’t really confirm. Maybe somebody will see the post and clarify if its correct for javascript.

hmm… maybe try this…

var skeleton = (Transform)Instantiate (sBlack, transform.position, transform.rotation);
skeleton.name = skeleton.name.Replace(" (clone)", “Skeleton_Black”);

Sorry overlooked that last post. That looks like javascript now! Still getting a semi-colon error, I don’t see what is wrong so I guess that last post still applies. I think something is funny with the (Transform) part of the first line.
All of the code:

var sBlack : Transform;
var sRed : Transform;
private var keepSpawning : boolean = true;

function Start () 
{

if (keepSpawning == true)
{
Spawn();
}
}

function Spawn()
{
	yield WaitForSeconds (2);
	//Instantiate (sBlack, transform.position, transform.rotation);
	var skeleton : Transform = (Transform)Instantiate (sBlack, transform.position, transform.rotation);
    skeleton.name = skeleton.name.Replace(" (clone)", "Skeleton_Black");


	//yield WaitForSeconds (2);
	//Instantiate (sRed, transform.position, transform.rotation);
	keepSpawning = false;


}

try the last suggestion without the casting and specification of type.

I don’t know what either of those things that you are referring to are.

var skeleton : Transform = (Transform)Instantiate (sBlack, transform.position, transform.rotation);
to
var skeleton : Transform = Instantiate (sBlack, transform.position, transform.rotation) as Transform

or lose transform altogther because US is silly with typing
var skeleton = Instantiate (sBlack, transform.position, transform.rotation);

That change makes the error go away, however, the prefab is still instantiating with (clone) on its name.

the search text is case sensitive.

make sure its (cloned) or (Cloned) or (clone) or whatever it actually says. I cant remember off the top of my head.

alternatively, just ignore the replace code and set the name fully

skeleton.name = “skeleton_black”;

That was it! Script is working perfect now and I don’t have to go through 80 characters and add “(Clone)” to their name lol. Thank you guys for helping out, saved me loads of time. This community is awesome.

This is great, would the code be the same for c#?

Yes, besides replacing “function” with “method”.