0

Я столкнулся с проблемой по созданию системы очков. Делал их в Update(), а также создал GameManager, т.к. взаимодействую ещё с другим сценами. Столкнулся с тем, что очки очень быстро прибавляются. Очки должны прибавляться один раз, а не бесконечность, т.е. если ответ правильный, то плюс 2.8 очка. Я новичок и хотел бы попросить помощи. Окажите мне помощь, пожалуйста.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

 public class Excersice12 : MonoBehaviour
 {
private GameObject Manager;

public Dropdown dropdown1;

public Dropdown dropdown2;

public Dropdown dropdown3;

public Dropdown dropdown4;

public Dropdown dropdown5;

public Dropdown dropdown6;

public Dropdown dropdown7;

public Dropdown dropdown8;

void Start()
{
    Manager = GameObject.Find("GameManager").gameObject;

    dropdown1.GetComponent<Dropdown>().value = 0;

    dropdown2.GetComponent<Dropdown>().value = 0;

    dropdown3.GetComponent<Dropdown>().value = 0;

    dropdown4.GetComponent<Dropdown>().value = 0;

    dropdown5.GetComponent<Dropdown>().value = 0;

    dropdown6.GetComponent<Dropdown>().value = 0;

    dropdown7.GetComponent<Dropdown>().value = 0;

    dropdown8.GetComponent<Dropdown>().value = 0;
}

void Update()
{
    int menuIndex1 = dropdown1.GetComponent<Dropdown>().value;
    if (menuIndex1 == 0 || menuIndex1 == 1 || menuIndex1 == 2 || menuIndex1 == 3)
    {
       // Manager.GetComponent<GameManager>().score = Manager.GetComponent<GameManager>().score - 1;
    }
    else if (menuIndex1 == 4)
    {
        Manager.GetComponent<GameManager>().score = Manager.GetComponent<GameManager>().score + 2.8;
    }

    int menuIndex2 = dropdown2.GetComponent<Dropdown>().value;
    if (menuIndex2 == 0 || menuIndex2 == 1 || menuIndex2 == 3 || menuIndex2 == 4)
    {
       // Manager.GetComponent<GameManager>().score = Manager.GetComponent<GameManager>().score - 1;
    }
    else if (menuIndex2 == 2)
    {
        Manager.GetComponent<GameManager>().score = Manager.GetComponent<GameManager>().score + 2.8;
    }

    int menuIndex3 = dropdown3.GetComponent<Dropdown>().value;
    if (menuIndex3 == 0 || menuIndex3 == 1 || menuIndex3 == 2 || menuIndex3 == 4)
    {
       // Manager.GetComponent<GameManager>().score = Manager.GetComponent<GameManager>().score - 1;
    }
    else if (menuIndex3 == 4)
    {
        Manager.GetComponent<GameManager>().score = Manager.GetComponent<GameManager>().score + 2.8;
    }

    int menuIndex4 = dropdown4.GetComponent<Dropdown>().value;
    if (menuIndex4 == 0 || menuIndex4 == 1 || menuIndex4 == 3 || menuIndex4 == 4)
    {
       // Manager.GetComponent<GameManager>().score = Manager.GetComponent<GameManager>().score - 1;
    }
    else if (menuIndex4 == 2)
    {
        Manager.GetComponent<GameManager>().score = Manager.GetComponent<GameManager>().score + 2.8;
    }

    int menuIndex5 = dropdown5.GetComponent<Dropdown>().value;
    if (menuIndex5 == 0 || menuIndex5 == 1 || menuIndex5 == 2 || menuIndex5 == 3)
    {
       // Manager.GetComponent<GameManager>().score = Manager.GetComponent<GameManager>().score - 1;
    }
    else if (menuIndex5 == 4)
    {
        Manager.GetComponent<GameManager>().score = Manager.GetComponent<GameManager>().score + 2.8;
    }

    int menuIndex6 = dropdown6.GetComponent<Dropdown>().value;
    if (menuIndex6 == 0 || menuIndex6 == 1 || menuIndex6 == 2 || menuIndex6 == 4)
    {
       // Manager.GetComponent<GameManager>().score = Manager.GetComponent<GameManager>().score - 1;
    }
    else if (menuIndex6 == 3)
    {
        Manager.GetComponent<GameManager>().score = Manager.GetComponent<GameManager>().score + 2.8;
    }

    int menuIndex7 = dropdown7.GetComponent<Dropdown>().value;
    if (menuIndex7 == 0 || menuIndex7 == 1 || menuIndex7 == 2 || menuIndex7 == 4)
    {
       // Manager.GetComponent<GameManager>().score = Manager.GetComponent<GameManager>().score - 1;
    }
    else if (menuIndex7 == 3)
    {
        Manager.GetComponent<GameManager>().score = Manager.GetComponent<GameManager>().score + 2.8;
    }

    int menuIndex8 = dropdown8.GetComponent<Dropdown>().value;
    if (menuIndex8 == 0 || menuIndex8 == 1 || menuIndex8 == 3 || menuIndex8 == 4)
    {
       // Manager.GetComponent<GameManager>().score = Manager.GetComponent<GameManager>().score - 1;
    }
    else if (menuIndex8 == 2)
    {
        Manager.GetComponent<GameManager>().score = Manager.GetComponent<GameManager>().score + 2.8;
    }

    /*if (0 > Manager.GetComponent<GameManager>().score)
    {
        Manager.GetComponent<GameManager>().score = 0;
    }*/
   }
}
8
  • А как быстро они должны прибавляться? Чего конкретно вы хотите? 31 июл 2019 в 21:26
  • и несколько советов по коду: вместо кучи переменных используйте массив, и работайте с ним циклом, также не делайте много вызовов GetComponent если знаете что это один и тот же компонент, первым вызовом сохраните в переменную и с ней работайте дальше 31 июл 2019 в 21:28
  • trollingchar, очки должны прибавляться один раз, а не бесконечность, т.е. если ответ правильный, то плюс 2.8 очка.
    – Maksim
    31 июл 2019 в 21:39
  • тогда вам в апдейте не надо прибавлять очки, прибавляйте только когда пользователь примет ответ один раз, апдейт он каждый кадр вызывается 1 авг 2019 в 6:08
  • trollingchar, извините, что я настолько туго понимаю, но а где можно прибавлять очки тогда? В Start() или Awake()?
    – Maksim
    1 авг 2019 в 6:19

1 ответ 1

1

Честно сказать сложно понять что вы писали там в коде. Я его так понял.
У вас есть Dropdown который будет что то типа варианты ответов, так? И вы хотите чтобы если пользователь выбрал один из них то получил ответ либо правильно (+2.8 очков) либо нет. Крайне не советую в этом использовать Dropdown (используйте Toggle с компонентом ToggleGroup) но если именно так задан ваш вопрос, чтож...

float score;
public Dropdown Answers;
//Тут через инспектор вам надо подключить Dropdown

private void Start()
{
    //Тот что снизу называется слушатель. Он проверяет был ли изменён значение(value) в Dropdown-е
    Anwsers.onValueChanged.AddListener(delegate
    {
        if(Answers.value == 2) //В место 2 должен быть цифра правильного ответа
        {
            Debug.Log("That's right!");
            score += 2.8f;
        }
        else
        {
            Debug.Log("Эх");
        }
    });
}


Но если будете слушать меня и использовать ToggleGroup то вам повезло, потому что на вопрос "Как писать событие для ToggleGroup" уже есть ответ.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.