Search Unity

mesh bounds is giving inaccurate results

Discussion in 'Scripting' started by All_American, Jan 21, 2020.

  1. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    I have an object getting imported into my scene at 0, 0, 0 and then it is moved into another position. Using this code.

    Code (CSharp):
    1. foreach (Renderer gmeshr in transform.GetComponents<Renderer>())
    2.             {
    3.                 float sizey;
    4.                 Vector3 modelcenter;
    5.                 sizey = gmeshr.bounds.max.y;
    6.                 modelcenter = gmeshr.bounds.size;
    7.                 Data_Canvas.transform.localPosition = new Vector3(gmeshr.bounds.center.x, gmeshr.bounds.center.y, sizey + 2.5f);
    8.                 //Debug.Log("Model Height: " + sizey);
    9.                 //Debug.Log("Model size: " + modelcenter);
    10.             }
    I display an icon above the object and it works fine.

    Then I serialize the object, and when the app is opened again the object is where it is supposed to be but the icon is the same distance away from the object as the object is from 0, 0, 0.

    I isolated it to being this code. But before the serialization it works fine.

    I parent the icon to the object after it is instantiated. Using SetParent, true.
     
  2. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    I fixed it by changing line 7 to this.

    Code (CSharp):
    1. Data_Canvas.transform.position = new Vector3(gmeshr.bounds.center.x, sizey + 2.5f, gmeshr.bounds.center.z);