xs0mE1
xs0mE1

Reputation: 79

Remove (Clone) name from gameobject spawned

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();

        }
    }
}

Upvotes: 1

Views: 2134

Answers (1)

Chris McFarland
Chris McFarland

Reputation: 6169

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();

Upvotes: 2

Related Questions