I am making a trading card game where someone clicks on a card back, and it displays a random card.
I imported a spritesheet with my cards sliced into sprites. I imported a cardback.
I dragged the cardback to the Hierarchy menu, set a box collider, and then pulled in the cards as children. I added a new asset that was an animation controller. In the animator tab, I set up states for all the cards. I right clicked and made transitions to and from all the states. Then I clicked on each card in the Hierarchy and clicked the Animation tab. Then I pulled up the sprite from Project into the first frame of the animator and saved the new animation. Then I went back to the cardback Animator and pulled all the animations into the Inspector of the states. I set an Int parameter at the bottom of the animator called curState. Then I clicked the arrows in the animator to add the curState.
I clicked on cardback in the Hierarchy and clicked Add Component in the Inspector. I pulled the cardback animator with all the states into the Inspector.
Screenshot of cardback.
Screenshot of child.
I clicked Add component and added a new script:
using UnityEngine;
using System.Collections;
public class ClickToChange : MonoBehaviour {
//components
protected SpriteRenderer spriteRenderer;
protected Animator animator;
public static int fighter = 0;
public static int fighter2 = 1;
public static int mage = 2;
public static int ranger = 3;
public static int ringofdeflection = 4;
public static int staffoflife = 5;
public static int swordofwar = 6;
public static int theif = 7;
void Start ()
{
spriteRenderer = gameObject.GetComponent<SpriteRenderer> ();
animator = gameObject.GetComponent<Animator> ();
}
void OnMouseDown()
{
Debug.Log ("Clicked");
int randomNumber = Random.Range (0, 7);
switch(randomNumber)
{
case 0: setState (fighter); break;
case 1: setState (fighter2); break;
case 2: setState (mage); break;
case 3: setState (ranger); break;
case 4: setState (ringofdeflection); break;
case 5: setState (staffoflife); break;
case 6: setState (swordofwar); break;
case 7: setState (theif); break;
}
}
public int setState (int state)
{
Debug.Log ("Changed");
this.animator.SetInteger ("curState", state);
return state;
}
}
But when I run my game, the last card I dragged into the Hierarchy just sits there and sort of blinks. It does add Clicked and Changed to the log appropriately however.
Does anyone know how to fix this? Also, are there any steps I can save or don’t need in the UI?