Search code examples
c#unity-game-engineprefab

Why does the position of my prefab is changed when dropped in the scene?


I have a problem, I made a prefab of a cube that I want to place on a platform. The y value of my prefab is 1. However, when I try to place it on the platform the cube takes the y value of 0.5 which puts it inside of the ground. I tried to reset the transform in the prefab, the ground and the cubes but none of that worked.

Screenshots of my unity window with both the prefab and how its value is changed.


Solution

  • I'm assuming you're dragging the prefab into the Scene window without using code or anything like that.

    Why does the position get modified

    The reason your cube is halfway into the ground likely is due to Unity trying to have it centered on the Y axis on the ground object you have in the scene since that's likely the object you're dragging your prefabs on.

    Solution 1

    If you wish to drag things into the Scene window but wish to avoid the cubes going into the ground, one solution is to change the center position of the cubes. One way of doing that is by putting the cube into an empty GameObject and modify the cube's position inside it and use this GameObject as a prefab. Here's a screenshot illustrating how your modified version of the prefab would look like:
    enter image description here

    Solution 2

    If wish to move a cube into the scene without any position modifications, drag it into the Hierarchy window instead.

    Solution 3

    Create a custom script that modifies the Y position of your cube prefab instance. OnValidate() may be something worth looking into for this solution.