Card placing problem for simple 2d card game.

Hello, I’m having trouble with a simple 2d card game i’m making.

I have gameobjects to represent cards (all cloned from first one) and 5 gameobjects to represent fields on which the cards can be placed if they are unoccupied.
Here is the code

void OnMouseUp()
	{
		for (int i = 0; i < 5; i++) 
		{
			GameObject currentCardField = GameObject.Find("CardField" + i.ToString());
			Debug.Log (currentCardField.GetInstanceID());
			CardFieldAttributes currentFieldAttributes = currentCardField.GetComponent("CardFieldAttributes") as CardFieldAttributes;

			if (gameObject.renderer.bounds.Intersects(currentCardField.renderer.bounds) && !currentFieldAttributes.fieldTaken) 
			{
				gameObject.transform.position = currentCardField.transform.position;
				currentFieldAttributes.fieldTaken = true;
				break;
			} 
			else 
			{
				gameObject.transform.position = pocetno;
			}
		}
		Screen.showCursor = true;
	}

Now the problem is it works as intended but only for CardField0, others just won’t work and i have no idea what the problem might be. It’s most probably something silly but I’m just baffled here…

as i said all fields and cards are clones so all settings apart from their names are the same.

What is the gameObject this script is attached to? Is it possible that the gameObject's bounds are always intersecting CardField0?

@Baalhug sorry i forgot to mention it pocetno is my variable that holds initial card location so it returns to its start position if it's not dropped on field or that field is occupied.

2 Answers

2

Scratch first answer. Just noticed a problem I didn’t see before.

Your if statement in the loop is saying that, if the card does not intersect the currentCardField bounds, the card should return to it’s original position. So the card is returned as soon as Intersects returns false (on CardField0). You are moving the card before any other CardField bounds can be checked, so it will never intersect with CardFields 1-4.

You only want to move the card to the original position if it doesn’t intersect ANY fields. This should work:

if (gameObject.renderer.bounds.Intersects(currentCardField.renderer.bounds) &&
!currentFieldAttributes.fieldTaken) 
{
  gameObject.transform.position = currentCardField.transform.position;
  currentFieldAttributes.fieldTaken = true;
  break;
} 
else if (i == 4) // last Card Field did not intersect, so move it back
{
  gameObject.transform.position = pocetno;
}

thank you for suggestion, i will try that tomorrow and report back if it worked for me. edit: damn, i knew it would be something simple... thanks man!

No problem!

Well, due to the problem itself i’m pretty sure the for loop is always true, so it always casts the break line. Have you tried to debug it?

yeah it behaves somewhat peculiar, but i think it might have something to do with all objects being cloned or something. debug shows false but card wouldn't stick to the field, then sometimes on CardField0 it shows false but the card still doesn't stick to field, when put there again it showed true etc. i will try hamstar's suggestion, since this code looks ok to everyone.