I am sure this answer is already somewhere on the forums but I just can’t seem to word it correctly in the search for me to find it.
So if any of you got a moment, I appreciate it! ![]()
Building a quick game for my iPad where I touch a slot to rotate through three card options. An attack card, charge card, and defend card.
I want to be able to tap the empty slot and have the attack card instantiate, tap again to destroy the attack card prefab and replace it with the charge card, tap it a third time to destroy the charge and instantiate the defend card prefab.
Then I would want it to simply repeat the process.
I figure I need some sort of variable that will count how many times I got the raycast to collide with the slot and determine which prefab to instantiate, but the problem I am running into is destroying the previous prefab while instantiate anything else but the attack prefab.
I took out of the code below the destroy object part of the script since unity didn’t want to do it for the reason “Destroying assets is not permitted to avoid data loss.”
So here is the code I got running so far:
var cardA : Transform;
var cardC : Transform;
var cardD : Transform;
function Update () {
// Use Raycast to pick objects that have mesh colliders attached.
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
//Returns true during the frame the user touches the object
if (collider.Raycast (ray, hit, 100)) {
Debug.DrawLine (ray.origin, hit.point);
if (Input.GetMouseButtonDown (0)) {
Instantiate(cardA, transform.position, transform.rotation);
}
// if (Input.GetMouseButtonDown (0)) {
// Instantiate(cardC, transform.position, transform.rotation);
// }
// if (Input.GetMouseButtonDown (0)) {
// Instantiate(cardD, transform.position, transform.rotation);
// }
}
else {
}
}
Obviously the issue here is that it will just keep instantiating the attack card over and over again each time I tap the slots. I have left the code written that way, so I am not surprised that it is the outcome of this code.
I am curious what my next step is to PROPERLY instantiate the next card and destroy the previous.
I got an ugly idea how I can get the next card to instantiate but no idea how to get rid of the previous card prefab.
So, any advice on how to cleverly rotate between these three card options on a simple tap mechanic?
Thanks a million for the time here! ![]()
