Reputation: 159
I'd like to clone a object and set it as child of another object using the following code:
spawnClone = Instantiate(other.gameObject, position, rotation, plate.transform) as GameObject;
Destroy(spawnClone.GetComponent<Collider>());
//spawnClone.transform.SetParent(plate.transform);
spawnClone.GetComponent<Rigidbody>().isKinematic = true;
spawnClone.SetActive(true);
Unfortunatly the parenting doesn't work and the cloned object is just placed in the root of the scene. If I try to just move the clone object into any other object in the hierarchy, it just gets out of it again right away (while playing).
Is there anything im doing wrong or that I don't see?
Thanks in advance!
EDIT: For a better understanding
I have a gameobject (plate) that has a child object(trigger). The trigger has a mesh collider (trigger enabled) and a script. The script has the following OnTriggerEnter-Method:
private void OnTriggerEnter(Collider other)
{
if (burgerGameManager.gameElement.Contains(other.gameObject.tag)) {
burgerGameManager.addToPlayerOrder(other.gameObject);
other.gameObject.SetActive(false);
Vector3 position;
if (transform.childCount > 1) {
Transform lastChild = transform.GetChild(transform.childCount - 1);
position = lastChild.position;
} else {
position = plate.transform.position;
position.y = position.y + plate.GetComponent<MeshRenderer>().bounds.size.y;
}
spawnClone = Instantiate(other.gameObject, position, other.gameObject.transform.rotation, gameObject.transform) as GameObject;
Destroy(spawnClone.GetComponent<Collider>());
//spawnClone.transform.SetParent(plate.transform);
spawnClone.GetComponent<Rigidbody>().isKinematic = true;
spawnClone.SetActive(true);
}
}
This method is supposed to trigger when the player drops a object above the plate. That object should hide and spawn a clone of it to appear on the plate.
Later on that script is supposed to be attached to the spawned clone aswell, so that the player can stack objects on the plate. But I did not yet come to that since I didn't even get the first step working as we can see...
Upvotes: 0
Views: 1188
Reputation:
spawnClone.transform.parent = plate.transform;
Should do it, if the plate is supposed to be the parent
Edit
I've recreated your code as follows
public class Test : MonoBehaviour{
private void OnTriggerEnter2D(Collider2D other)
{
Vector2 position;
other.gameObject.SetActive(false);
position = this.gameObject.transform.position;
position.y += 0.5f;
if (transform.childCount >= 1)
{
Transform lastChild = transform.GetChild(transform.childCount - 1);
position = lastChild.transform.position;
position.y += 0.5f;
}
GameObject spawnClone = Instantiate(other.gameObject, position, Quaternion.identity);
Destroy(spawnClone.GetComponent<Collider2D>());
spawnClone.GetComponent<Rigidbody2D>().isKinematic = true;
spawnClone.transform.parent = this.gameObject.transform;
spawnClone.SetActive(true);
}
}
And I got this behaviour, which is what I think you want
My only conclusion here is that
burgerGameManager.addToPlayerOrder(other.gameObject);
may be messing with the parenting. Maybe try moving this at the end of the function and see what happens?
Upvotes: 1