- Home /
Spawning a prefab at another object's location
Hi there, I'm doing a project that involves a cube breaking up into smaller pieces, i'm doing this by deleting the cube and replacing it with a prefab made of smaller cubes to give it that breaking effect.The problem i'm having is that the prefab is spawning at the wrong location and i'm not sure where to change it.
Here is the code i'm using , its done in C#
using UnityEngine;
using System.Collections;
public class BlockDestory : MonoBehaviour
{
public GameObject remains;
void Update ()
{
if (Input.GetKey (KeyCode.Space))
{
Instantiate(remains,transform.position,transform.rotation);
Destroy(gameObject);
}
}
}
I've also added some pics of the inspector incase you need them.
Thanks /D
The code has been edited, thanks for the link. It has some very good tips for future questions.
Answer by Scorpox · Apr 13, 2015 at 11:09 PM
I see that your Fragments prefab is a group. Is the pivot of this group centered on the object? If not, drag the prefab to the scene, then select it and click on GameObject in the upper menu. There you'll se an option that says "Center on children". Click on that option and apply the changes to the prefab. You can now delete it from the scene.
I don't say this is the solution, but it could be one solution. ;D
Regards
Answer by glennhk · Apr 14, 2015 at 02:23 AM
Be sure that the prefabs are always positioned at (0,0,0).
Answer by lordlycastle · Apr 14, 2015 at 03:59 AM
The problem is that every smaller cube in the group is not positioned at centre of the group. When you click the child you can see in the inspector that it’s position is not (0,0,0)
(The second pic). Change that to zero then it should spawn in correct poison. Also I see that have rigid body on the cubes so you’ll have to destroy the big cube first then spawn the smaller ones. To do that simply cache the poison to a local variable and destroy, then spawn.