vendredi 13 juillet 2018

If statement or for loop

I have a method that is being called, every time a user touches the screen. Once they have touched the screen enough times, the object that the script is attached to is deactivated.

The script in question is attached to layouts that I am looping out based on dynamic data.

So say I have 10 questions, that means I will have 10 layouts with 10 versions of the script attached.

I am not sure I am taking the best approach, but I am stuck and as I am new to Unity/C# I was hoping for some guidance.

I need the script to check a different index, based on the screen index. Currently, the if statements (in the method below) work perfectly, however, as I don't know many screens there will be (due to dealing with dynamic data), I need a way of doing these checks without a huge list of if statements.

I understand that I can use a for loop, but my problem is that I need the count to change depending on the screen index (as shown in my if statement). Is it possible to do what I am after with loops, or do I need to change the way I am approaching this?

A problem I am facing is that the only constant available between adding the new script to each instance is the ScreenIndex count variable. Hence why I am repeated referring to this in my if statements.

I tried wrapping my method in a for (ScreenIndex = 0; ScreenIndex < UIHandler.GetdynamicQuestionArray().Count; ScreenIndex += 4) but the count goes out of sync. Will I have to use nestled loops? I have tried but without success so far.

My method currently looks like this:

//Scratch pad 1 - Due to this method being called everytime a pixel is scratched, nestled if statements need to be used.
public void OnEraseProgressScratch1(float progress)
{
    //Set a count of i, to make sure the screen index can be checked
    int i = 0;

    //While loop to help condition to make sure we know what page the user is currently on, keep looping i until it matches the index the user is on.
    //This is necessary because i will keep getting reset everytime this method is called, so the while loop will make sure i matches the index
    while (i != transform.parent.gameObject.transform.parent.gameObject.GetComponent<TeamQuizScreen>().ScreenIndex)
    {
        i++;
    }
    //Once the user has scratched more than 50% of the pad, deactivate it
    if (Mathf.Round(progress * 100f) >= 50)
    {
        //If the user has scratched more than 50% stop the user from scratching any further and check if they are correct.
        transform.parent.gameObject.transform.GetChild(0).GetChild(3).GetChild(0).GetChild(0).gameObject.SetActive(false);
        //Loop through the array that holds if an answer is correct or not

        //If the screen index is the same as the page number
        if (transform.parent.gameObject.transform.parent.gameObject.GetComponent<TeamQuizScreen>().ScreenIndex == i)
        {
            int ScreenIndex = transform.parent.gameObject.transform.parent.gameObject.GetComponent<TeamQuizScreen>().ScreenIndex;

            if (ScreenIndex == 0)
            {
                Debug.Log("X (should be 0) = " + ScreenIndex);
                //If the user is at screen index 0, then a loop should be querying position 0 only here. If the user is at position 1 then a loop should be 
                //querying position 4 - this is because position 1,2,3 will be queried in 'OnEraseProgressScratch2', 'OnEraseProgressScratch3' and 'OnEraseProgressScratch4'.
                if (UIHandler.GetDynamicCorrectAnswerArray()[ScreenIndex].ToString().Contains("Correct"))
                {
                    Debug.Log("Answer 1 is correct");
                }

            }
            else if(ScreenIndex == 1)
            {
                ScreenIndex += 3;

                Debug.Log("X (should be 4) = " + ScreenIndex);

                if (UIHandler.GetDynamicCorrectAnswerArray()[ScreenIndex].ToString().Contains("Correct"))
                {
                    Debug.Log("Answer 1 is correct");
                }

            }
            else if (ScreenIndex == 2)
            {
                ScreenIndex += 6;

                Debug.Log("X (should be 8) = " + ScreenIndex);
                if (UIHandler.GetDynamicCorrectAnswerArray()[ScreenIndex].ToString().Contains("Correct"))
                {
                    Debug.Log("Answer 1 is correct");
                }
            }
            else if (ScreenIndex == 3)
            {
                ScreenIndex += 9;

                Debug.Log("X (should be 12) = " + ScreenIndex);

                if (UIHandler.GetDynamicCorrectAnswerArray()[ScreenIndex].ToString().Contains("Correct"))
                {
                    Debug.Log("Answer 1 is correct");
                }
            }
            else if (ScreenIndex == 4)
            {
                ScreenIndex += 12;

                Debug.Log("X (should be 16) = " + ScreenIndex);
                if (UIHandler.GetDynamicCorrectAnswerArray()[ScreenIndex].ToString().Contains("Correct"))
                {
                    Debug.Log("Answer 1 is correct");
                }

            }
            else if (ScreenIndex == 5)
            {
                ScreenIndex += 15;
                Debug.Log("X (should be 20) = " + ScreenIndex);

                if (UIHandler.GetDynamicCorrectAnswerArray()[ScreenIndex].ToString().Contains("Correct"))
                {
                    Debug.Log("Answer 1 is correct");
                }
            }
            //This will need to continue based on the number of questions in the hierarchy
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire