Search Unity

I seem to have written a script which effects the transform of my prefabs (bug, I assume)

Discussion in 'Scripting' started by gobguy, Dec 17, 2014.

  1. gobguy

    gobguy

    Joined:
    Jan 7, 2014
    Posts:
    14
    I wrote a script that runs at start and utilizes the FindObjectsOfTypeAll(typeof(GameObject) function to iterate through all of the objects in the scene and adjust their position and scale based on the screen's aspect ratio. When I press play it seems to work well enough, but when I press stop the game objects don't always return to their original positions. Not only that, but I find that the transforms of the prefabs linked to those objects have also been changed. I can't reload the scene and get back their original positions, the change is permanent.

    So... what's up with that? This seems like a bug to me. Here's the relevant bit of code:


    Vector3 deltaWorld = urMarker - blMarker;

    Vector3 deltaScreen = urScreen - blScreen;

    float xRatio = Mathf.Abs (deltaScreen.x / deltaWorld.x);

    float zRatio = Mathf.Abs (deltaScreen.z / deltaWorld.z);

    GameObject[] allObjects = (GameObject[])Resources.FindObjectsOfTypeAll(typeof(GameObject));

    foreach (GameObject anObject in allObjects)
    {
    Transform anObjectTransform = anObject.transform;

    float deltaMarkerX = anObjectTransform.position.x - blMarker.x;
    float deltaMarkerZ = anObjectTransform.position.z - blMarker.z;

    Vector3 newPosition = Vector3.zero;

    newPosition.y = anObjectTransform.position.y;
    newPosition.x = (deltaMarkerX * xRatio) + blScreen.x;
    newPosition.z = (deltaMarkerZ * zRatio) + blScreen.z;

    anObjectTransform.position = newPosition;
    }
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    BenZed likes this.
  3. gobguy

    gobguy

    Joined:
    Jan 7, 2014
    Posts:
    14
    I chose FindObjectsOfTypeAll because I wanted to include disabled objects. Specifying "typeof(GameObject)" was supposed to exclude the prefabs, among other things, since prefabs are not game objects. Clearly that's the problem line, you're right, but I don't see why it's a problem.
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Because the Resources method is designed to give you everything. It's not limited to just the scene whereas the Object method does not return assets.

    You're getting prefabs back because they are functionally treated as GameObjects.

    I'd recommend moving this logic into a script that affects a single GameObject and call it in Awake or Start or OnEnable or moving all the GameObjects affected by this under a single parent and traversing the hierarchy that way.
     
  5. tenth

    tenth

    Joined:
    Nov 27, 2012
    Posts:
    5
    Hm. Well the point of the script was to capture everything in the scene, including all the inactive stuff. In the long run I think I might have to go with the resources method but add some additional logic to ensure that everything else really is excluded... Regardless, you've given me enough to work with it in the short term. So: thanks.