- Home /
Unable to remove (Clone) suffix (with a possibly a related instantiation issue)
I am trying to create a new instance of a prefab, but it keeps appending the "(Clone)" suffix to the end of the new copies. I tried looking at similar questions, but none of them matched what I was trying to do.
Here's the code I'm working with:
void generateChain(){
GameObject newChain = Resources.Load("ChainPrefab") as GameObject;
Rope2 newScript = newChain.GetComponent<Rope2>();
newScript.PointA = GameObject.Find("Sphere");
newScript.PointB = GameObject.Find("Point B");
newScript.ropeDetail = (int) Vector3.Distance(GameObject.Find("Point B").transform.position, GameObject.Find("Sphere").transform.position);
Rope2.AttachPoint spherePosition = new Rope2.AttachPoint();
spherePosition.attachedObject = this.gameObject;
newScript.attachedObjects.Clear();
newScript.attachedObjects.Add(spherePosition);
Instantiate(newChain);
newChain.name = "Chain";
}
As you can see, at the end I do change the newChain GameObject's name to "Chain," but this doesn't do anything.
On a possibly related note, am I instantiating a new copy of the prefab correctly? I wonder because as you see above I modify some of its scripts prior to instantiating, as the chain itself is rendered upon instantiation, and needs to be set up prior to then. Is this the correct way to go about that?
Answer by DaveA · Mar 17, 2012 at 12:14 AM
var nc = Instantiate(newChain);
nc.name = "Chain";
Getting closer. That solves the issue of Chain's name, though now I'm still having an issue where the rope generated in association with the chain object is being named "Chains(Clone)_Rope". The rope is named with the following code:
ropeObject = new GameObject(transform.name + "s_Rope");
It is part of the RopeScript made by Reverie Interactive here. I tried changing that code to:
GameObject tempChain = Instantiate(newChain) as GameObject;
tempChain.transform.name = "Chain";
But this didn't seem to fix it. Is there some other part of the object that needs to be renamed for it to work properly?
@transplanar: Where do you create the ropeObject? In a script that is part of the chain prefab? If that's the case make sure you do it in Start() and not in Awake().
Awake is called even before Instantiate() returns so you have no chance to rename the object from outside. Another way (which is the best i guess) is to rename the object from an attached scripts Awake function.
void Awake()
{
name = "Chain";
}