Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ChestHole · Feb 06, 2014 at 08:06 PM · 2drandomspritesswitchonmousedown

OnMouseDown generates random sprite

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ChestHole · Feb 07, 2014 at 03:22 AM

I forgot to add a box collider to the cardback. The edited above process now works.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Shape Changing Sprites 0 Answers

How to tile sprite in 2D game when scaling instead of stretching 2 Answers

Memory Game Question 1 Answer

How to spawn randomly positioned objects without overlapping 2 Answers

2D Top-Down 3D-Like effect 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges