- Home /
The question is answered, right answer was accepted
Remove (clone) from Instantiated Gameobjects name
I am looking for a way to not have an instantiated gameobject carry the (clone) after its name.
Right now I add two prefabs to viking1 and viking2. but no matter how i do it in the script that Instantiates,the name of the gameobjects always have the (Clone) after its name. Once its instantiated.
If I set the name in the GameObjects script, it will rename it correctly, but thats not a real option, since several gameobjects use this same script.
public class ControllerScript : MonoBehaviour { public GameObject viking1; public GameObject viking2;
void Awake()
{
Vector3 pos = transform.position;
pos.x = transform.position.x + 2;
Instantiate(viking1, pos, Quaternion.identity); //Instantiating Viking1.
pos.x = transform.position.x - 2;
Instantiate(viking2, pos, Quaternion.identity); //Instantiating Viking2.
}
// Use this for initialization
void Start () {
viking2.name = "Viking2";
viking1.name = "Viking1";
}}
Not sure how to do a C# script but for JS script you can try this:
var viking = Instantiate(viking1, pos, Quaternion.identity); viking.name = "Viking2";
Answer by Mike 3 · Sep 25, 2010 at 02:58 PM
You need to set the name of the instantiated gameobject, not the two items you're using to instantiate.
e.g.
GameObject newViking1 = (GameObject)Instantiate(viking1, pos, Quaternion.identity); //Instantiating Viking1.
newViking1.name = viking1.name;
Perfect, this solved my problem, I dont know why I did not think of this.. Am a bit new to program$$anonymous$$g, thats probably why ;) Thanks again
It still doesn't work for me. Debug says that the name is 3 but the inspector says it's 3(clone) which is why I still cannot convert it to an integer.
It probably means you are not referencing the correct object @mattockman
Answer by Tabu · Sep 25, 2010 at 06:11 PM
Here is the working code:
void Awake() { Vector3 pos = transform.position; pos.x = transform.position.x + 2; GameObject newViking1 = (GameObject)Instantiate(viking1, pos, Quaternion.identity); //Instantiating Viking1. newViking1.name = "Viking1";
pos.x = transform.position.x - 2;
GameObject newViking2 = (GameObject)Instantiate(viking2, pos, Quaternion.identity); //Instantiating Viking2.
newViking2.name = "Viking2";
}
Follow this Question
Related Questions
Assign Names Problem 1 Answer
Instantiate Name Issue. 1 Answer
Get instantiated name even before loading the asset from bundle 0 Answers
Naming Instantiated Prefabs 2 Answers