Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by calebjross · Sep 04, 2021 at 09:34 PM · scripting problemparentingboxcollider2dz-axisonmouseover

Some Box Collider 2Ds seem to shift on some instances of a prefab

I've been banging my head against the desk for hours. I hope someone can help me with this.

The issue is: Some Box Collider 2Ds aren't clickable, even though those Box Colliders are objects on instances from the same parent prefab.

I'm newish to Unity and C#, but I've tried to be as detailed as possible below. This is my first time posting a question here, so I hope I'm doing it adequately.

First, here's a video showing the issue. As you can see, when I hover over each of the card stats on the card, the yellow triangle sprite appears, as expected. This behavior is consistent for the first two cards in the stack. However, on the third card, only the bottom two card stats operate according to my expectation.

https://youtu.be/vFu0Q2kXrN4

I checked to see if the OnMouseOver() method was even reacting to the first 4 card stats on card three. It isn't. So, for some reason OnMouseOver() stops behaving as expected. This is especially strange considering that the Box Collider 2Ds do appear to be active and in the correct position (according to the Scene view during run-time, see image below)

alt text

I then thought that maybe the z position of the cards themselves are causing a complication, but even when I set all of the z positions to 0, the issue persists.

Also, I tried adjusting the x and y positions of each card, just to search for different behavior, but the issue persists.

The cards are dealt at Start() from a GameManager script attached to the main camera:

 //player cards behavior
         Shuffle(playerCards);
         pxpos = 6f;
         pypos = -1f;
         pzpos = 0f;
         for (int i = 0; i < playerCards.Length; i++)
         {
             playerCards[i].tag = "PlayerCard";
             Instantiate(playerCards[i], new Vector3(pxpos, pypos, pzpos), Quaternion.identity);
             pxpos -= 0.5f;
             pypos += 0.5f;
             pzpos -= 0.1f;
         }
 
         //computer cards behavior
         Shuffle(computerCards);
         cxpos = -6f;
         cypos = -1f;
         czpos = 0f;
         for (int i = 0; i < computerCards.Length; i++)
         {
             computerCards[i].tag = "ComputerCard";
             Instantiate(computerCards[i], new Vector3(cxpos, cypos, czpos), Quaternion.identity);
             cxpos += 0.5f;
             cypos += 0.5f;
             czpos -= 0.1f;
         }
         playerCardScore = playerCards.Length;
         computerCardScore = computerCards.Length;
     }

The cards themselves are instances of a prefab (see image below). Each card stat is a child under the main card object. Each child has it's own Box Collider 2D that's meant to register the OnMouseOver(). alt text

Here's the OnMouseOver() code.

 public void OnMouseOver()
 {
     Debug.Log("OnMouseOver is active");
     //determines which stat value to select from the player card
     if (cardBehavior.isFaceUp && cardBehavior.isPlayerTopCard)
     {
         spriteRenderer.enabled = true;
         computerTopCard = cardBehavior.GetComputerTopCard();
         switch (gameObject.name)
         {
             case "stat0":
                 playerStat = cardBehavior.stat0;
                 computerStat = computerTopCard.GetComponent<CardBehavior>().stat0;
                 break;
             case "stat1":
                 playerStat = cardBehavior.stat1;
                 computerStat = computerTopCard.GetComponent<CardBehavior>().stat1;
                 break;
             case "stat2":
                 playerStat = cardBehavior.stat2;
                 computerStat = computerTopCard.GetComponent<CardBehavior>().stat2;
                 break;
             case "stat3":
                 playerStat = cardBehavior.stat3;
                 computerStat = computerTopCard.GetComponent<CardBehavior>().stat3;
                 break;
             case "stat4":
                 playerStat = cardBehavior.stat4;
                 computerStat = computerTopCard.GetComponent<CardBehavior>().stat4;
                 break;
             case "stat5":
                 playerStat = cardBehavior.stat5;
                 computerStat = computerTopCard.GetComponent<CardBehavior>().stat5;
                 break;
             default:
                 playerStat = 0;
                 break;
         }
         //Debug.Log("Player " + gameObject + " of " + playerStat + " vs Computer " + computerTopCard + " of " + computerStat);
     }



I'm absolutely at a loss. Any help is much appreciated!

prefab.png (123.6 kB)
boxcolliders.png (132.2 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by calebjross · Sep 05, 2021 at 01:31 AM

I MAY have figured this out. There seems to be some conflict with the gameObject BoxCollider2D and the parent BoxCollider2D.

To address this I simply disabled the parent BoxCollider2D while the card is face up (which is the only time an OnMouseOver() on the card stats needs to register). I'm not confident this is the most elegant way of solving it, but it at least works.

If anyone has a more elegant solution or wants to correct what I think is the cause of the error, please let me know.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

223 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

drag and drop then attach child to parent 0 Answers

Unity Mouse Look Script Issue 0 Answers

Coin collection script (not working) 1 Answer

Lock rotation of player 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges