- Home /
Check if Object is in a Position? (Using Compare tag)
Hello, I am currently looking into how to check if an object is in a position before creating another instance of an object in that same position. Basically, my game objects are cards that are all in the scene, if a card is clicked on, I want the card to go on top of the scene into a position. However, if the position is already occupied by a card, I would like it to occupy the next position. I can figure out the if/else coding for the most part, but I am racking my brain trying to figure out how to establish whether the position is already taken by another card. I was suggested to use Raycasting (and compare tags), but there are multiple rows of cards that the ray would go straight through before reaching the top portion of the scene. Is there another way I can use the Compare Tag to get the result I am looking for? Or is there an entire other way I could try? Please let me know if you need any clarification. All tips/advice is GREATLY appreciated! Thanks! - Comic
Answer by ComicGirl · Jun 28, 2017 at 05:57 PM
I was able to find a work around that seems to work for now. I will post what my script looks like here so that it may be of use to others. I'm very open to suggestions on how to optimize it. Thanks! - Comic
private static bool holder1Occupied = false; // Variable True/False if Card Occupies a position
private static bool holder2Occupied = false;
private static bool holder3Occupied = false;
private static bool holder4Occupied = false;
private static bool holder5Occupied = false;
private static bool holder6Occupied = false;
void OnClickClone() // Check if Position is Occupied by a Card
{
if (holder1Occupied == false)
{
Debug.Log("Cloning Process Initiated");
Instantiate(wordCard, sentenceHolder[0].transform.position, sentenceHolder[0].transform.rotation); // If position is not occupid create new instance of card
holder1Occupied = true;
}
else if (holder2Occupied == false)
{
Instantiate(wordCard, sentenceHolder[1].transform.position, sentenceHolder[1].transform.rotation);
holder2Occupied = true;
}
else if (holder3Occupied == false)
{
Instantiate(wordCard, sentenceHolder[2].transform.position, sentenceHolder[2].transform.rotation);
holder3Occupied = true;
}
else if (holder4Occupied == false)
{
Instantiate(wordCard, sentenceHolder[3].transform.position, sentenceHolder[3].transform.rotation);
holder4Occupied = true;
}
else if (holder5Occupied == false)
{
Instantiate(wordCard, sentenceHolder[4].transform.position, sentenceHolder[4].transform.rotation);
holder5Occupied = true;
}
else if (holder6Occupied == false)
{
Instantiate(wordCard, sentenceHolder[5].transform.position, sentenceHolder[5].transform.rotation);
holder6Occupied = true;
}
else
{
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
SetActive is not working 2 Answers
transform.SetParent(null); Not Working 1 Answer