I am trying to create a simple VR card game in Unity using OpenXR, just to learn Unity and also some VR stuff. I made a deck of cards object and a card object. I put both in a scene. The card is Kinematic and can be grabbed and thereby picked up. The deck is static on the table. I put the card on top of the deck and I can pick it up (see https://drive.google.com/file/d/1gi4RAUDzS-r862I6ur3eb_YZPQRXjMMA/view?usp=sharing). However I want to spawn a new card on top of the deck to grab. First I wanted to spawn the card, first when the user makes the grab action, touching the pile, but I could not find any information how to do that and actually attach the card (instead of the deck) to the hand. How can I detect the grab of my playing card, and spawn a new card on top of the deck instead of the one just taken by a player? I am an experienced software developer, but I am a total noob when it comes to Unity. I tried to find some answers online, but I think I lack some basic understanding of how Unity works. I hope someone can point me in the right direction.
With the help of "Pow" I have found a solution.
I created a property on my Card object:
public GameObject deckPrefab;
Then dragged the deck object from the scene onto this property to assign the deck object ot the deckPrefab property.
Next when the card is grabbed. I instanciate a new card on top of the deck:
GameObject newCard = Instantiate(cardPrefab, new Vector3(deckPrefab.transform.position.x, deckPrefab.transform.position.y + 0.01f, deckPrefab.transform.position.z), Quaternion.identity);
The card has a property, which card it is (taken from a list of all cards remaining in the deck). And the face of the card is replaced with a new texture:
var renderer = newCard.GetComponentInChildren<Renderer>();
for (int i=0; i<renderer.materials.Length; i++)
var material = renderer.materials[i];
int idx = GetCardIndex(newCard.textureIndex);
cardFaces is an array of Texture2D objects with all the PNG images of the card faces. I used this guide on YouTube for inspiration, but modified to use 3D objects instead of sprites: https://www.youtube.com/playlist?list=PLuq_iMEtykn8XiZb6453f61U7HC7u8KiC