Search Unity

gameobject scale mismatch issue

Discussion in 'Scripting' started by twangsta, Apr 13, 2013.

  1. twangsta

    twangsta

    Joined:
    Mar 28, 2013
    Posts:
    17
    Hi,

    I'm trying to create a level editor. Creating an empty container called section that contains prefabs called segments which are instances of a prefab of a wall (cube).

    When I write the code as such it works fine:
    Code (csharp):
    1. function make_room_F ()
    2. {
    3.     var section : GameObject = Instantiate(section_fab, mousePos, Quaternion.identity);
    4.     section.name = "section_F" + appState.sectionCount++;
    5.     //back right wall
    6.     var wall_back : GameObject = Instantiate(wall_fab, mousePos, Quaternion.identity);
    7.     wall_back.name = "wall_front";
    8.     wall_back.transform.parent = section.transform;
    9.     var wall_obj : GameObject  = getChild('name', 'Wall', wall_back);
    10.     var length : float = wall_obj.transform.localScale.z;
    11.     wall_back.transform.position = Vector3(length/4, 0, length/2);
    12.     wall_back.transform.localScale.x = wall_back.transform.localScale.x/2;
    13.     wall_back.AddComponent("overlay");
    But if I generalise it to something like this:
    Code (csharp):
    1. function fabricate_segment ( name:String, length:float, position:Vector3, parent:GameObject ) : GameObject
    2. {
    3.     // create wall segment
    4.     var segment : GameObject = Instantiate(wall_fab, mousePos, Quaternion.identity);
    5.  
    6.     segment.name = name;
    7.     segment.transform.localScale.x = length;
    8.     segment.transform.position = position;
    9.     segment.transform.parent = parent.transform;
    10.     return segment;
    11. }
    12.  
    13.  
    14. function make_room_G ()
    15. {
    16.     var section : GameObject = Instantiate(section_fab, mousePos, Quaternion.identity);
    17.     section.name = "section_" + (appState.sectionCount++) + "_G";
    18.    
    19.     var wall_top: GameObject = fabricate_segment( "wall_top", 20, Vector3(0,0,1/2), section);
    20.         ...
    21.  
    The resulting wall segment still indicates the value I've given it in the object inspector of the "Wall" child object but is like drawn like 10 times larger when I'm using the function.

    I'm new to unity, I apologise if I've made an obvious mistake.

    Thanks in advance,
    Sid
     
  2. JeanSimonet

    JeanSimonet

    Joined:
    Nov 23, 2012
    Posts:
    31
    In the first code you halved the x scale:
    Code (csharp):
    1. wall_back.transform.localScale.x = wall_back.transform.localScale.x/2
    but in the second code, you make it 20 (you pass it as length and assign it in the function).
    I'm assuming the starting scale is 1, so you would have a difference of 40
     
  3. twangsta

    twangsta

    Joined:
    Mar 28, 2013
    Posts:
    17
    Hi Jean, thanks for replying.

    That's not actually making a difference, the original value is 40 of the Wall prefab in wall_fab. I was trying different things, sorry it wasn't explicit.

    First example length is then 40/2, second example it's explicitly 20. Should be the same, as it's revealed in the inspector, both are 20 but they're drawn v very different :confused:

    Any other suspects in there?

    Sid
     
  4. twangsta

    twangsta

    Joined:
    Mar 28, 2013
    Posts:
    17
    Turns out I was scaling a parent and expecting the chid to respect the absolute value.

    Unity 3D's scaling for a child object will be multiplied, so the fractions worked but not the absolute values.

    edit: A second before I figured this out I thought it might be 'a pass by value' issue, thankfully this is not the case here.