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);
}
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
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…
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");
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.
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;
}
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.