Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Prefab generate position Z

Discussion in 'Scripting' started by antoniomiguelferreiraaires, May 17, 2018.

  1. antoniomiguelferreiraaires

    antoniomiguelferreiraaires

    Joined:
    Apr 10, 2018
    Posts:
    53
    Hi I have this code to generate 4 prefabs initially but the middlefloors can grow in number:

    Code (CSharp):
    1. public Transform bottomFloorClose;
    2.     public Transform middleFloor01Close;
    3.     public Transform middleFloor02Close;
    4.     public Transform topFloorClose;
    5.     public Transform TowerClose;
    6.  
    7.     public int numberOfFloors = 4;
    8.     private float positionXClose = 0.25f;
    9.     private float positionYClose = -11.11f;
    10.     private const float floorHeigthClose = 13.45f;
    11.  
    12.     void Start()
    13.     {
    14.         GenerateTower(numberOfFloors);
    15.     }
    16.    
    17.     private void GenerateTower(int numberOfFloors)
    18.     {
    19.         TowerClose.transform.localScale = new Vector3(0.78f, 0.78f, 0F);
    20.  
    21.         int numberOfMiddleFloors = numberOfFloors - 2;
    22.  
    23.         Transform bottomFloorObj = Instantiate(bottomFloorClose, new Vector3(positionXClose, positionYClose, 0f), Quaternion.identity);
    24.         bottomFloorObj.transform.SetParent(gameObject.transform, false);
    25.         positionYClose += floorHeigthClose;
    26.  
    27.         List<Transform> floorList = new List<Transform> { middleFloor01Close, middleFloor02Close };
    28.         Random random = new Random();
    29.  
    30.         for (int i = 0; i < numberOfMiddleFloors; i++)
    31.         {
    32.             int index = random.Next(floorList.Count);
    33.             Transform middleFloorObj = Instantiate(floorList[index], new Vector3(positionXClose, positionYClose, 0f), Quaternion.identity);
    34.             middleFloorObj.transform.SetParent(gameObject.transform, false);
    35.             positionYClose += floorHeigthClose;
    36.         }
    37.  
    38.         Transform topFloorObj = Instantiate(topFloorClose, new Vector3(positionXClose, positionYClose, 0f), Quaternion.identity);
    39.         topFloorObj.transform.SetParent(gameObject.transform, false);
    40.     }
    41. }
    Their order is not correct, I need to have the bottomfloor like at z=0 and middlefloor1 and middlefloor2 at z=-10 and z=-20 so that topfloor will be z=-30 to be in the correct order so the sprites fit well I tried to changing their position in the inspector didn't work. Tried in the GenerateTower script via transform.position but didn't work.

    Any ideas? Thanks
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,395
    So are you saying that :
    1. Floors appear with the correct scale and rotation. -- AND --
    2. The overall position of the Tower itself is correct in world space. -- AND --
    3. The position of the individual floors within the Tower is inverted?
    Or are the top and bottom floors appearing correctly with only the middle floors being wrong? If so, wrong in what way?

    Or something else? :)
     
  3. antoniomiguelferreiraaires

    antoniomiguelferreiraaires

    Joined:
    Apr 10, 2018
    Posts:
    53
    1. Floors have correct scale and rotation
    2. The overall position of the tower composed by the different sprites (bottomfloor, middelfloors etc) is correct
    3. The position of individual floors are correct in X and Y axis, not on Z

    This might seem confusing but the wrong thing/the real problem is that the bottomfloor, middlefloors and topfloor don't have the Z position because they need to be in a certain order of depth.
    Imagine a puzzle, you can't put A on top of B because only B fits on top of A so you have to put A with a lower value of Z so B fits on top of it. It's the same with my gameobjects
     
  4. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,395
    I didn't pick up on the fact it was the Z axis that was wrong. In that case, your problem lies in these bits of code Vector3(positionXClose, positionYClose, 0f). You are setting the X and Y axis values but the Z is always zero.

    At least, I can't see anywhere in your code that is setting the Z component. :)
     
  5. antoniomiguelferreiraaires

    antoniomiguelferreiraaires

    Joined:
    Apr 10, 2018
    Posts:
    53
    Yes.. I've tried something like:
    Code (CSharp):
    1.  Transform middleFloorObj = Instantiate(floorList[index], new Vector3(positionXClose, positionYClose, -10f), Quaternion.identity);
    But then in the inspector it still stays at the same 0 Z position :confused:
     
  6. antoniomiguelferreiraaires

    antoniomiguelferreiraaires

    Joined:
    Apr 10, 2018
    Posts:
    53
    Also, after the prefabs are created as Clones if I change the view to 3D in the scene it don't let me move them in the Z axis!
     
  7. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,395
    So do you have another piece of code somewhere that is resetting the z to be zero?

    Do you mean, you move them but they get reset? Or are they actually greyed out in the editor showing that they cannot be moved at all? What about moving them along X and Y - does that work?
     
  8. antoniomiguelferreiraaires

    antoniomiguelferreiraaires

    Joined:
    Apr 10, 2018
    Posts:
    53
    I think the problem is here TowerFar.transform.position = new Vector3(15, 0, 0) or here Transform bottomFloorObjFar = Instantiate(bottomFloorFar, new Vector3(positionXFar, positionYFar, 0f), Quaternion.identity) because I'm setting the Z at 0 and then I can't change the prefab Z order..

    In the inspector they move in X and Y but not in Z, it's like locked on Z
     
  9. antoniomiguelferreiraaires

    antoniomiguelferreiraaires

    Joined:
    Apr 10, 2018
    Posts:
    53
    I alrealdy solved it using the prefab order in layer.
     
  10. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,395
    Glad to hear you solved it. :)