Search Unity

Problem with [Destroying object] Instantiating the correct rotation.

Discussion in 'Scripting' started by i3artyy2222, Oct 9, 2015.

  1. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    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 explanation.png


    Here is the code of my destructible glass
    Code (JavaScript):
    1.  
    2.  
    3. var boxdead : boolean;
    4. var boxdeadprefab : Transform;
    5. function Start () {
    6.     boxdead = false;
    7. }
    8.  
    9.     function Update(){
    10.         if(health <= 0){
    11.  
    12.             if(!boxdead)
    13.             {
    14.                 boxdead = true;
    15.  
    16.                 Instantiate(ParticleOnDestroy, transform.position, Quaternion.identity);
    17.                 DestroyOb();
    18.             }
    19.          
    20.         }
    21.     }
    22.    
    23.    
    24.     function DestroyOb ()
    25.    
    26.     {
    27.         Destroy(gameObject, destroy.length);
    28.         Instantiate(boxdeadprefab, transform.position,boxdeadprefab.transform.rotation);
    29.     }
    Thanks for helping!
     
  2. Thomas-Mountainborn

    Thomas-Mountainborn

    Joined:
    Jun 11, 2015
    Posts:
    501
    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.
     
    Kiwasi and i3artyy2222 like this.
  3. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    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 :/
     
    Last edited: Oct 9, 2015
  4. Thomas-Mountainborn

    Thomas-Mountainborn

    Joined:
    Jun 11, 2015
    Posts:
    501
    How is the prefab structured? Does it have any other scripts running on it that set its rotation?
     
  5. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    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 :D Thanks !
     
    Thomas-Mountainborn likes this.