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:
function make_room_F ()
{
var section : GameObject = Instantiate(section_fab, mousePos, Quaternion.identity);
section.name = "section_F" + appState.sectionCount++;
//back right wall
var wall_back : GameObject = Instantiate(wall_fab, mousePos, Quaternion.identity);
wall_back.name = "wall_front";
wall_back.transform.parent = section.transform;
var wall_obj : GameObject = getChild('name', 'Wall', wall_back);
var length : float = wall_obj.transform.localScale.z;
wall_back.transform.position = Vector3(length/4, 0, length/2);
wall_back.transform.localScale.x = wall_back.transform.localScale.x/2;
wall_back.AddComponent("overlay");
But if I generalise it to something like this:
function fabricate_segment ( name:String, length:float, position:Vector3, parent:GameObject ) : GameObject
{
// create wall segment
var segment : GameObject = Instantiate(wall_fab, mousePos, Quaternion.identity);
segment.name = name;
segment.transform.localScale.x = length;
segment.transform.position = position;
segment.transform.parent = parent.transform;
return segment;
}
function make_room_G ()
{
var section : GameObject = Instantiate(section_fab, mousePos, Quaternion.identity);
section.name = "section_" + (appState.sectionCount++) + "_G";
var wall_top: GameObject = fabricate_segment( "wall_top", 20, Vector3(0,0,1/2), section);
...
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