Okay, So I have an script LevelGenerator.cs that generates multiple objects called Rooms (Its attached to the Main Camera so is called on creation). Each Room object has a BuildRoom.cs script attached to it that builds a room made of cube primitives and then creates a rectangle around the entire boundry. I’m trying to get the rectangle created into the level generator script, but it keeps returning a Rectangle of (0, 0, 0, 0).
LevelGenerator.cs
using UnityEngine;
using System.Collections;
public class GenerateLevel : MonoBehaviour {
private int rooms = 10;
private int x = 0;
private int y = 0;
private GameObject room;
GameObject[] roomList;
private BuildRoom buildScript;
private BuildRoom buildScriptTwo;
private Rect rect;
private Rect rectTwo;
// Use this for initialization
void Start () {
roomList = new GameObject[rooms];
room = (GameObject)Resources.Load("Room");
for(int i = 0; i < rooms; i++){
roomList[i] = (GameObject)Instantiate(room, new Vector3(x, y, 0), Quaternion.identity);
Debug.Log (roomList[i].GetComponent<BuildRoom>().getRectangle());
x = Random.Range (0, 200);
y = Random.Range (0, 200);
}
}
// Update is called once per frame
void Update () {
}
}
BuildRoom.cs
using UnityEngine;
using System.Collections;
public class BuildRoom : MonoBehaviour {
private Vector3 position;
public Rect rectangle;
public int roomWidth;
public int roomHeight;
Mesh objectMesh;
private Bounds bounds;
private float offsetX;
private float offsetY;
float x;
float y;
float z = 0;
// Use this for initialization
void Start () {
position = transform.localPosition;
roomWidth = Random.Range(6, 18);
roomHeight = Random.Range (6, 18);
rectangle = new Rect(transform.localPosition.x - 1.5f, transform.localPosition.y - 1.5f, roomWidth + 3.5f, roomHeight + 3.5f);
//Draw left side
for(int i = 0; i < roomHeight + 1; i++){
GameObject block = GameObject.CreatePrimitive(PrimitiveType.Cube);
block.AddComponent<Rigidbody>();
block.transform.position = new Vector3(position.x,position.y + i, 0);
block.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
block.transform.parent = transform;
objectMesh = block.GetComponent<MeshFilter>().mesh;
bounds = objectMesh.bounds;
}
//Draw right side
for(int i = 0; i < roomHeight + 1; i++){
GameObject block = GameObject.CreatePrimitive(PrimitiveType.Cube);
block.AddComponent<Rigidbody>();
block.transform.position = new Vector3(position.x + roomWidth + 1,position.y + i, 0);
block.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
block.transform.parent = transform;
}
//Draw bottom
for(int i = 1; i < roomWidth + 1; i++){
GameObject block = GameObject.CreatePrimitive(PrimitiveType.Cube);
block.AddComponent<Rigidbody>();
block.transform.position = new Vector3(position.x + i, position.y, 0);
block.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
block.transform.parent = transform;
}
//Draw top
for(int i = 0; i < roomWidth + 2; i++){
GameObject block = GameObject.CreatePrimitive(PrimitiveType.Cube);
block.AddComponent<Rigidbody>();
block.transform.position = new Vector3(position.x + i,position.y + roomHeight + 1, 0);
block.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
block.transform.parent = transform;
}
}
public Rect getRectangle(){
return rectangle;
}
public int getRoomWidth(){
return roomWidth;
}
public int getRoomHeight(){
return roomHeight;
}
// Update is called once per frame
void Update () {
position = transform.localPosition;
rectangle = new Rect(transform.localPosition.x - 1.5f, transform.localPosition.y - 1.5f, roomWidth + 3.5f, roomHeight + 3.5f);
//Draw left line
Debug.DrawLine(new Vector3(rectangle.x, rectangle.y, 0), new Vector3(rectangle.x, rectangle.y + rectangle.height, 0));
//Draw right line
Debug.DrawLine (new Vector3(rectangle.x + rectangle.width, rectangle.y, 0), new Vector3(rectangle.x + rectangle.width, rectangle.y + rectangle.height, 0));
//Draw top
Debug.DrawLine (new Vector3(rectangle.x, rectangle.y + rectangle.height, 0), new Vector3(rectangle.x + rectangle.width, rectangle.y + rectangle.height, 0));
//Draw bottom
Debug.DrawLine (new Vector3(rectangle.x, rectangle.y, 0), new Vector3(rectangle.x + rectangle.width, rectangle.y, 0));
}
}
Why does it not return the correct rectangle?