I have created a game object (Brick) and added a script (BrickScript) to it. Within this script I create a Window game object if a certain condition is satisfied. Please see code below:
if (hasWindow) {
if (window == null) {
var size = gameObject.GetComponent<SpriteRenderer>().bounds.size;
window = (GameObject)Instantiate (Resources.Load ("Prefabs/OtherPrefabs/Window"),
new Vector3 (transform.position.x + 0.5 * size.x,
transform.position.y + 0.5f * size.y,
transform.position.z),
Quaternion.Identity));
window.name = gameObject.name + "Window";
}
}
The Brick and window game objects actually overlap a bit. I realized that when hasWindow = true (the window is created) the condition in the code below is not satisfied and other game objects don't collide with the Brick. But when hasWindow = false (the window is not created), everything seems to work well. How can I solve this?
void OnMouseDown () {
if (Input.GetMouseButtonDown(0)) {
Debug.Log ("touch down");
hasBeenTouched = true;
}
}
windowis created and theBrickdoesn't collide with other game objects though it has aBox Collider 2Dand is colliding with objects with rigid bodies \$\endgroup\$size. It's not a good idea to do GetComponent multiple times, instead it's best to cache it. \$\endgroup\$