1
votes

So what I'm trying to do is spawn a prefab of smaller cubes onto the exact same position and rotation of a big cube so it 'breaks apart'. When I hit space, the big cube disappears as intended, but the smaller cubes instantiate from a distance.

Here's a .gif of what is happening.

 void Update () {
         if (Input.GetKeyDown("space"))
         {
             Instantiate(explo, gameObject.transform.position, Quaternion.identity);
             Destroy(gameObject);
         }
         Debug.Log("The Rotation");
         Debug.Log(explo.transform.rotation);
         Debug.Log("The Position");
         Debug.Log(explo.transform.position);
     }

Debug Log:

 The Rotation
 (0.0, 0.0, 0.0, 1.0)
 The Position
 (-5.5, -2.4, 3.5)

This is an extremely simple script and I've spent 2 days trying to find a similar problem to mine, but they all include Quaternion, which is not similar to my problem.

EDIT: Changing gameObject.transform.rotation to Quaternion.identity seems to entirely reset the rotation of the object.

EDIT2: 'explo' is defined before 'void start' as 'public GameObject explo', and is assigned via the Unity Editor.

1
What is explo defined as and what game object do you have this script attached to?Ben Rubin
The script is attached to an object titled "Big Cube", and 'explo' is a prefab that I attached via the unity editor, it's called "Smaller Cubes".Hellie Charty
Do you have any scripts attached to explo? Is this your exact code? I'm confused how explo.localScale compiles because I think it should be explo.transform.localScaleBen Rubin
explo has no scripts attached to it, and changing explo.localScale to explo.transform.localScale still returns the same result. (If there was an error before I hadn't noticed, it still compiled without error?)Hellie Charty
A side note (I don't think this is related to the problem); Debug.Log(explo.transform.position); is showing the position of your prefab, not of the position of the instance of that prefab that you added to your scene. If you want to see the position of the object in your scene, you want something like GameObject cube = Instantiate(explo, gameObject.transform.position, Quaternion.identity) as GameObject; and then show the position of cube.Ben Rubin

1 Answers

1
votes

Check the position of the child cubes in your "explosion" prefab. From the screenshot and logs that you posted, it looks your prefab is positioned at (-5.5, -2.4, 3.5) and the smaller cubes within your prefab are positioned close to that. When you're looking at your prefab in isolation, it looks fine since all of your smaller cubes are positioned around the center of your prefab. However, your smaller cubes will retain their prefabbed positions when you instantiate your prefab in your scene. So if you place your prefab at (0, 0, 0) when you're running your game, the child cubes will still positioned nearby (-5.5, -2.4, 3.5). Positioning all of the small cubes within your prefab relative to (0, 0, 0) will make them show up where you expect them to when you instantiate your prefab anywhere in your scene.

Another thing: the line Debug.Log(explo.transform.position); is actually showing the position that you assigned to your prefab within the Unity editor, not the position of the prefabbed object that got instantiated within your scene. To show the position of the object in your scene, store a reference to the instantiated object, and then show the position of that object. Like this

GameObject gameObject = Instantiate(explo, gameObject.transform.position, gameObject.transform.rotation) as GameObject;
Debug.Log(gameObject.transform.position);