Search Unity

Prefab takes over wrong position

Discussion in 'Scripting' started by InteractivePie, Apr 16, 2015.

  1. InteractivePie

    InteractivePie

    Joined:
    May 2, 2014
    Posts:
    18
    I'm experiencing a strange issue with prefabs. This is what is happening:

    I have a gameobject Player. The hierarchy is like this:
    -Player
    --> head
    --> body
    ----> legs
    -------> upper leg
    etc. so every part of the body is a separate gameobject. (every part moves separately)
    The entire player object is a prefab. with one instance in the scene.
    I made a PlayerBehaviour script. like this:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerBehaviour : MonoBehaviour {
    5.     public GameObject player;
    6.  
    7.     public void Respawn() {
    8.         GameObject temp = Instantiate (player, Vector3.zero, Quaternion.identity) as GameObject;
    9.     }
    10.  
    11.     // Update is called once per frame
    12.     void Update () {
    13.         if (Input.GetKeyDown(KeyCode.Space)) {
    14.             Respawn ();
    15.         }
    16.     }
    17. }
    18.  
    the public gameobject player has the prefab assigned NOT the instance in the scene.
    My problem is (when pressing space). The instantiated prefab should have been positioned at 0.0.0. (including child gameobjects should be at their prefab position) The Player gameobject spawns indeed at 0.0.0 but the moving body parts from the old instance of the object are copied at the exact same position as the newly spawned instance from the prefab. What did i do wrong?
     
  2. InteractivePie

    InteractivePie

    Joined:
    May 2, 2014
    Posts:
    18