Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 choosakul · Aug 26, 2015 at 12:48 PM · c#unity 5arraysprite

Store multiple sprite in code C#

I'm new with unity programming code and I using this answer in first time,so my English might bad and sorry about this.

I want to create matching game with sprite

  • has 3 level; level 1 has 4 question sprites, level 2 has 9 question sprites, level 3 has 19 question sprite. -each question has 3 choices but the correct answer is sprite that match to question sprite. -Sprites in answer have more than 1 sprite,but it will choose one of them

the main problem is my code was too specific for store both question and answer.

is this for level 1 code.

         randomAnswerLocate = new int[]{0,1,2};
 
         questionSpriteArrLv1 = Resources.LoadAll <Sprite> ("Question/Quiz1");
         questionSpriteArrLv2 = Resources.LoadAll <Sprite> ("Question/Quiz2");
         questionSpriteArrLv3 = Resources.LoadAll <Sprite> ("Question/Quiz3");
         answerspriteArr01 = Resources.LoadAll<Sprite> ("Answer/Answer1");
         answerspriteArr02 = Resources.LoadAll<Sprite> ("Answer/Answer2");
         answerspriteArr03 = Resources.LoadAll<Sprite> ("Answer/Answer3");
         answerspriteArr04 = Resources.LoadAll<Sprite> ("Answer/Answer4");
 
 
         ansPoint01 = GameObject.Find ("PostLeft");
         ansPoint02 = GameObject.Find ("PostCenter");
         ansPoint03 = GameObject.Find ("PostRight");
 
         inside1 = GameObjectUtil.Instance.FindChildGameObject (ansPoint01, "Ans").GetComponent<SpriteRenderer> ();
         inside2 = GameObjectUtil.Instance.FindChildGameObject (ansPoint02, "Ans").GetComponent<SpriteRenderer> ();
         inside3 = GameObjectUtil.Instance.FindChildGameObject (ansPoint03, "Ans").GetComponent<SpriteRenderer> ();
 
         scriptMousePoint1 = GameObjectUtil.Instance.FindChildGameObject (ansPoint01, "Ans").GetComponent<Mouse> ();
         scriptMousePoint2 = GameObjectUtil.Instance.FindChildGameObject (ansPoint02, "Ans").GetComponent<Mouse> ();
         scriptMousePoint3 = GameObjectUtil.Instance.FindChildGameObject (ansPoint03, "Ans").GetComponent<Mouse> ();

now, next is function that store sprite of question

 public int NextQuestionLv1(int indexQuestion){
 
         questionsprits = GameObject.Find ("Quiz").GetComponent<SpriteRenderer> ();
 
         if (indexQuestion == 0) {
         
             Sprite spriteQuiz = questionSpriteArrLv1 [questionAndAnswerList [indexQuestion].GetQuestionId ()];
             questionsprits.sprite = spriteQuiz;
             result = questionAndAnswerList [indexQuestion].GetQuestionId ();
             ShuffleArray (randomAnswerLocate);
 
             SetAnswer (0);
             SetAnswer (1);
             SetAnswer (2);
 
         }
 
         if (indexQuestion == 1) {
             Sprite spriteQuiz = questionSpriteArrLv1 [questionAndAnswerList [indexQuestion].GetQuestionId ()];
             questionsprits.sprite = spriteQuiz;
             result = questionAndAnswerList [indexQuestion].GetQuestionId ();
             ShuffleArray (randomAnswerLocate);
 
             SetAnswer (0);
             SetAnswer (1);
             SetAnswer (2);
         }
         if (indexQuestion == 2) {
             Sprite spriteQuiz = questionSpriteArrLv1 [questionAndAnswerList [indexQuestion].GetQuestionId ()];
             questionsprits.sprite = spriteQuiz;
             result = questionAndAnswerList [indexQuestion].GetQuestionId ();
             ShuffleArray (randomAnswerLocate);
 
             SetAnswer (0);
             SetAnswer (1);
             SetAnswer (2);
         }
         if (indexQuestion == 3) {
             Sprite spriteQuiz = questionSpriteArrLv1 [questionAndAnswerList [indexQuestion].GetQuestionId ()];
             questionsprits.sprite = spriteQuiz;
             result = questionAndAnswerList [indexQuestion].GetQuestionId ();
             ShuffleArray (randomAnswerLocate);
 
             SetAnswer (0);
             SetAnswer (1);
             SetAnswer (2);
         }

         return indexQuestion;

     }

next is function to store and calling answer sprite.

 public void SetAnswer(int indexAnswer)
     {
 
         int index = 0;
         index = indexQuestion + indexAnswer;
 
         if(index == 4)
         {
             index = 0;
         }else if(index == 5)
         {
             index = 1;
         }else if(index == 6)
         {
             index = 2;
         }
 
     
         if (questionAndAnswerList [index].GetAnswerId () == 0) {
             
             ShuffleArray (answerspriteArr01);
             if (indexAnswer == randomAnswerLocate [0]) {
                 inside1.sprite = answerspriteArr01 [0];
                 scriptMousePoint1.SetAnswerID (questionAndAnswerList [index].GetAnswerId ());
             } else if (indexAnswer == randomAnswerLocate [1]) {
                 inside2.sprite = answerspriteArr01 [0];
                 scriptMousePoint2.SetAnswerID (questionAndAnswerList [index].GetAnswerId ());
             } else if (indexAnswer == randomAnswerLocate [2]) {
                 inside3.sprite = answerspriteArr01 [0];
                 scriptMousePoint3.SetAnswerID (questionAndAnswerList [index].GetAnswerId ());
             }
             
         } else if (questionAndAnswerList [index].GetAnswerId () == 1) {
 
             ShuffleArray (answerspriteArr02);
             if (indexAnswer == randomAnswerLocate [0]) {
                 inside1.sprite = answerspriteArr02 [0];
                 scriptMousePoint1.SetAnswerID (questionAndAnswerList [index].GetAnswerId ());
             } else if (indexAnswer == randomAnswerLocate [1]) {
                 inside2.sprite = answerspriteArr02 [0];
                 scriptMousePoint2.SetAnswerID (questionAndAnswerList [index].GetAnswerId ());
             } else if (indexAnswer == randomAnswerLocate [2]) {
                 inside3.sprite = answerspriteArr02 [0];
                 scriptMousePoint3.SetAnswerID (questionAndAnswerList [index].GetAnswerId ());
             }
 
         } else if (questionAndAnswerList [index].GetAnswerId () == 2) {
             
 
             ShuffleArray (answerspriteArr03);
             if (indexAnswer == randomAnswerLocate [0]) {
                 inside1.sprite = answerspriteArr03 [0];
                 scriptMousePoint1.SetAnswerID (questionAndAnswerList [index].GetAnswerId ());
             } else if (indexAnswer == randomAnswerLocate [1]) {
                 inside2.sprite = answerspriteArr03 [0];
                 scriptMousePoint2.SetAnswerID (questionAndAnswerList [index].GetAnswerId ());
             } else if (indexAnswer == randomAnswerLocate [2]) {
                 inside3.sprite = answerspriteArr03 [0];
                 scriptMousePoint3.SetAnswerID (questionAndAnswerList [index].GetAnswerId ());
             }
             
         } else if (questionAndAnswerList [index].GetAnswerId () == 3) {
             
 
             ShuffleArray (answerspriteArr04);
             if (indexAnswer == randomAnswerLocate [0]) {
                 inside1.sprite = answerspriteArr04 [0];
                 scriptMousePoint1.SetAnswerID (questionAndAnswerList [index].GetAnswerId ());
             } else if (indexAnswer == randomAnswerLocate [1]) {
                 inside2.sprite = answerspriteArr04 [0];
                 scriptMousePoint2.SetAnswerID (questionAndAnswerList [index].GetAnswerId ());
             } else if (indexAnswer == randomAnswerLocate [2]) {
                 inside3.sprite = answerspriteArr04 [0];
                 scriptMousePoint3.SetAnswerID (questionAndAnswerList [index].GetAnswerId ());
             }
 
         }
 
     }

As you read it. It too long too , so I want suggestion from everyone how can I store many of them in level 2 and level 3.I glad for all your help!

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

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Sprite image not changing 0 Answers

Get an array of children from an existing array 1 Answer

Scrolling sprite image 0 Answers

How to change the sprite and the animator in script to make a character selection? 2 Answers

Problem with Line Renderer 1 Answer


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