3
\$\begingroup\$

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;
    }
}
\$\endgroup\$
  • \$\begingroup\$ What isn't working in the second part of code? You're checking for a mouse press, spitting something out and setting a bool to true. \$\endgroup\$ – n_plum Jul 6 '17 at 15:01
  • \$\begingroup\$ From your code, we can see that you are instantiating a Window prefab gameobject a above the brick and a bit offset on the X axis (depending on camera position). To answer the question, since everything appears to be working without the window object, we need to see the scene and know what other components are on the window property. It could be any number of things such as conflicting colliders, collision layer issues and so on. I think we can help you if you can provide a bit more context on what is going on in the scene to get an answer. \$\endgroup\$ – Ang Li Jul 6 '17 at 15:23
  • \$\begingroup\$ @n_palum As I mentioned, the if condition is not satisfied when a window is created and the Brick doesn't collide with other game objects though it has a Box Collider 2D and is colliding with objects with rigid bodies \$\endgroup\$ – SuperHyperMegaSomething Jul 6 '17 at 15:25
  • \$\begingroup\$ I've added an extra line in the edit, where i declared a variable called size. It's not a good idea to do GetComponent multiple times, instead it's best to cache it. \$\endgroup\$ – Shraa1 Sep 11 '18 at 13:30
0
\$\begingroup\$

It looks like the overlapping is causing your issue since the collider will be already colliding with the window. You can solve this problem a couple ways. you can create a new layer and set the window object to that layer. then in physics 2d settings you can set the collision matrix to not collide with each other. Or you can possibly set the windows z position to be in a spot that wont collide.

\$\endgroup\$
  • \$\begingroup\$ The if (hasWindow)... block is called in Start() and works without any issues. hasWindow is public, so if I want a brick to contain a window, I set it to true in the inspector. My issue is every brick that has a windowdoes not respond to mouse touches and collision with other game objects. Bricks without windows work perfectly. \$\endgroup\$ – SuperHyperMegaSomething Jul 6 '17 at 16:01
  • \$\begingroup\$ edited the answer to work as you intended \$\endgroup\$ – Justin Markwell Jul 6 '17 at 16:12
  • \$\begingroup\$ I want the brick with window to respond to OnMouseDown(). OnMouseDown() has no conditions related to the window so I don't understand why the brick will not respond because the window is not null. I would appreciate further explanation of this. \$\endgroup\$ – SuperHyperMegaSomething Jul 6 '17 at 16:23
  • \$\begingroup\$ my apologies for the confusion, I have the brick responding to OnMouseDown() even with the window instantiated so I assumed the window is what you wanted. I re-edited the answer after noticing you were overlapping the two game objects slightly. \$\endgroup\$ – Justin Markwell Jul 6 '17 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.