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

How to change an objects bounds? It's always ReadOnly...

Discussion in 'Scripting' started by MrDude, Aug 2, 2015.

  1. MrDude

    MrDude

    Joined:
    Sep 21, 2006
    Posts:
    2,569
    So I want to have a simply cube game object with a collider on it and pass to it any random mesh I choose.

    During Start I want it to instantiate the mesh I set and then change the cube (and the collider) to match the instantiated model's size, then make the model a child of this cube.

    the much simpler way would be to add a box collider to the object being instantiated as then it will have the correct size already but as it happens, the parent object has the physics code attached and the OnCollision and OnTriggerEnter code and all that fancy stuff that will not get triggered if it doesn't have a collider so I need the parent to match the size of the child.

    Since the child object will always have a scale of 1,1,1 setting the cube's scale is of no help and when I try to use MeshCollider.bounds or Collider.Bounds it tells me that Bounds read only so all the public setters I have access to are useless to me because I am accessing it via a read only variable.

    So here I am, trying to something as simple as making one box the same size as the other and I find myself clueless how to do something that sounds so simple...

    Can anyone please tell me how I can create a normal cube object from the main menu and then, at runtime, set it's size to match the bounds of another object? Right now my mesh is like 100 times smaller than the cube but has a scale of 1,1,1, so how do wrap the cube around the mesh?

    Thanks

    Here is what I am trying to do. The commented out line is moaning about bounds being read only and Encapsulate is useless because it is already larger than the projectile. I tried setting it to Vector3.zero first then calling it to encapsulate bounds but that just makes the child object's scale 0,0,0 (????????)

    This sound be such a simple thing and yet I have no idea how to do it... Please help me..
    Code (csharp):
    1.     Transform p = Instantiate<Transform>(projectile_prefab);
    2.     if (null == p)
    3.     {
    4.         Debug.LogError("Projectile not found");
    5.         return;
    6.     }
    7.  
    8.     p.position = transform.position;
    9.     Bounds bounds = p.gameObject.GetComponent<Renderer>().bounds;
    10.     MeshRenderer r = GetComponent<MeshRenderer>();
    11.  
    12. //  r.bounds.extents = bounds.extents;
    13.     r.bounds.Encapsulate(bounds);
    14.     p.SetParent(transform);
    15.     p.localRotation = Quaternion.identity;
    16.  
     
    Last edited: Aug 2, 2015
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    A cube primitive is 1x1x1, so wouldn't treating it's X/Y/Z scale like "width", "height", and "length" values give you the results you want?
     
  3. MrDude

    MrDude

    Joined:
    Sep 21, 2006
    Posts:
    2,569
    Sounds easy, don't it? So what doI set the x,y and z TO...?
    That's the million dollar question...

    both bounds.extents and bounds.size return 0,0,0.1... but then I remembered that debug.log rounds the values up so let me try it anyway...
    I import the Maya models into Unity using a scale of 0.01 and then in the scene it has a scale of 1,1,1 so at least that value of 0,0,0.1 sort of makes sense... So I tried this, just to test your theory...

    Code (csharp):
    1.     transform.localScale = bounds.extents * 2f;
    ...and it worked like a charm. Just need to work on fetching the correct starting offset now...

    I did not expect it to be such a simple solution yet was hoping for such. Thanks a lot.
     
    Alaadel likes this.