Home > Net >  Remove (Clone) name from gameobject spawned
Remove (Clone) name from gameobject spawned

Time:05-24

I have a game which spawns several objects but with the name (Clone) next to it, however I want to remove (Clone). Excuse me, I am new to Unity, and have tried several solutions but never worked.

I can send script file of instantiating objects if needed.

public void InstantiateObject()
{
    if(Input.GetMouseButtonDown(1))
    {
        Ray ray = cam.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if(Physics.Raycast(ray, out hit))
        {
            Instantiate(target, hit.point, Quaternion.identity);
            target.name = target.name.Replace("(Clone)","").Trim();

        }
    }
}

CodePudding user response:

Assuming target is a GameObject, you can declare a variable during instantiation, then change the instantiated object's name like so:

GameObject newTarget = Instantiate(target, hit.point, Quaternion.identity);
newTarget.name = newTarget.name.Replace("(Clone)","").Trim();
  • Related