- Home /
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.
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)
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().
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!
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.
Your answer
Follow this Question
Related Questions
Parent mesh problem 0 Answers
Collider2D Resize Based on Sprite Using Generics 1 Answer
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