Problem with [Destroying object] Instantiating the correct rotation.

Hi all, So i have a problem with Destroyable objects in my game, i have a plane, which is a glass and when the player hits it it instantiate prefab which is glass/window thats in pieces, but the problem is that it only works in one direction, if i rotate the glass for example 90 degrees and destroy it it destroys instantiate prefab as it wasnt rotate so it looks wrong. Here is photo to understand me better

Here is the code of my destructible glass

var boxdead : boolean;
var boxdeadprefab : Transform;
function Start () {
    boxdead = false;
}

    function Update(){
        if(health <= 0){

            if(!boxdead)
            {
                boxdead = true;

                Instantiate(ParticleOnDestroy, transform.position, Quaternion.identity);
                DestroyOb();
            }
          
        }
    }
    
    
    function DestroyOb ()
    
    {
        Destroy(gameObject, destroy.length);
        Instantiate(boxdeadprefab, transform.position,boxdeadprefab.transform.rotation);
    }

Thanks for helping!

You’re instantiating the prefabs with the rotation set in the prefab itself, which is why you get that result. Instead of using boxdeadprefab.transform.rotation, use transform.rotation. The prefabs will then be instantiated with the rotation of the game object the script is attached to, i.e. the window.

2 Likes

so i changed to this β€˜β€™ Instantiate(boxdeadprefab, transform.position, transform.rotation);β€˜β€™ and it stills does the same
Edit : i even tried to swaping destroy current game object which is glass to after it instantiates the broken glass but still same thing :confused:

How is the prefab structured? Does it have any other scripts running on it that set its rotation?

Hi, i have fixed it so what i did i checked if i rotate the prefab and apply will it change anything and it did but worked in one direction so i realized [long time i watched some tutorial about instantiate some prefab] so what i did i just selected each part in the prefab each child object of the prefab and rotated them so they face straight and clicked apply and now it works whatever direction i place the glass it destroys as it should :smile: Thanks !

1 Like