Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

All Imported Prefab's Children Meshes Have 0,0,0 Vector3s?

Discussion in 'Scripting' started by Rotavele, Jul 25, 2016.

  1. Rotavele

    Rotavele

    Joined:
    Jan 28, 2016
    Posts:
    29
    I apologize if this is a bit of an amateur question, it was pretty hard to get the wording right to search the answers/forums effectively.

    Basically I have imported a prefab with many gameObjects as children. The children GameObjects have mesh components as well as a manually added script.

    The problem I am having is that when trying to get the Vector3s (Via an array of scripts, then a foreach loop that finds gameObject.transform.position inside of it) they all return 0,0,0 because the transform on each gameObject is 0,0,0.

    Obviously they are located in different places on the screen, but how do I get their positions that they would be located on the screen?
     
  2. PhenixEmporium

    PhenixEmporium

    Joined:
    Jul 24, 2016
    Posts:
    22
    Getting the transform.position of the children should give you their world points, perhaps if you could give more details or the code you are using to access the children's positions, I could be more help.
     
  3. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    Located on the screen meaning you want their screen position? Or you mean you want there world position?
    You say the array has references to the individual scripts that are on the children? If so just try the foreach loop like this
    Code (CSharp):
    1. Foreach(NameOfScript s in nameOfArray)
    2. Debug.Log(s.transform.position);
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    One thing to note is depending on what version of unity you are using, I was having an issue in previous versions where getting the position during awake or start always returned 0,0,0. Unity confirmed it was a bug and emailed me later to say it was fixed in a newer version. You may consider upgrading to a newer version if that is your case, or you can get the position later. This is assuming you are doing this in awake or start to begin with.
     
  5. Rotavele

    Rotavele

    Joined:
    Jan 28, 2016
    Posts:
    29
    Sorry for the long reply, I've been searching many places and also trying different solutions. However i've had no luck. I've tried writing the script many different and even pretty elaborate ways, but that also brings no luck. So now its just very simple.

    World or Local position is fine as long as it's different.

    Here are pictures of the problem:





    As you can see they're def not all on the same point.

    Please ignore the If statements, I was a big frustrated and wanted to make it work haha.

    Code (CSharp):
    1.     [MenuItem("Region Tools/Get Vector3s")]
    2.     private static void GetVects()
    3.     {
    4.         GameObject Map = GameObject.Find("Regions");
    5.  
    6.         RegionScript[] Regions;
    7.  
    8.         Regions = Map.GetComponentsInChildren<RegionScript>();
    9.  
    10.         if (Regions != null)
    11.         {
    12.             Debug.Log("Found Regions for Region Vec Assignment");
    13.             foreach (RegionScript Region in Regions)
    14.             {
    15.                 // Vector3 RegionVec = Region.gameObject.transform.position;
    16.                 // Region.RegionVector3 = RegionVec;
    17.                 Region.LocalRegionVector3 = Region.gameObject.transform.localPosition;
    18.                 Region.WorldSpaceRegionVector3 = Region.gameObject.transform.position;
    19.  
    20.                 if(Region.LocalRegionVector3.x == 0 || Region.LocalRegionVector3.x == 5000)
    21.                 {
    22.                     Region.LocalRegionVector3 = Region.transform.localPosition;
    23.                     Region.WorldSpaceRegionVector3 = Region.transform.position;
    24.                 }
    25.  
    26.                 if (Region.LocalRegionVector3.y == 0 || Region.LocalRegionVector3.y == 5000)
    27.                 {
    28.                     Region.LocalRegionVector3 = Region.transform.localPosition;
    29.                     Region.WorldSpaceRegionVector3 = Region.transform.position;
    30.                 }
    31.             }
    32.         } else {
    33.                 Debug.log("Error getting regions!");
    34.        }
    35. }
     
  6. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    Do their positions change when you manually drag them around?
     
  7. Rotavele

    Rotavele

    Joined:
    Jan 28, 2016
    Posts:
    29
    Yes they do. I was thinking they're setting their pivot or something to 0,0,0. However i'm not that familiar with 3D unity so I have not looked for a way into that. I've been trying to write a system to just give them rays and colliders and write the data to a file to load during editor time.

    I'm looking at matrix4x4s now, but they do not seem to solve the problem any better.
     
  8. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    If you know where they need to be, maybe just force them to the correct coordinates?
    Like spawn them at the correct position
     
  9. Rotavele

    Rotavele

    Joined:
    Jan 28, 2016
    Posts:
    29
    I'm not sure what you mean? My main problem is that I want to calculate the distance between some of the meshes.
     
  10. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    I mean, what if you spawn one of the meshes at the position you want it to be, does it then still say 0,0,0? Like in GameObject.Instantiate(...)
     
  11. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    Ah and i overlooked the part about the pivot, which 3d program did you use? Have a look in there and center each pivot to its object.