Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

        }
    }
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading