- Home /
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!
Your answer
Follow this Question
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