F
F
Farset2018-05-11 12:27:58
C++ / C#
Farset, 2018-05-11 12:27:58

Passing array values ​​to a coroutine - C# - How to do it?

using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using System.Collections;
using System;
 
public class MainScript : MonoBehaviour {
    //Переменные
    private float score;//Очки
    private float scoreIncrease = 1;//Бонус к очкам
    private int LevelOfItem;
    private float bonusPerSec;
 
    //Arrays
    public float[] perSecondBonus;
    public float[] ItemCosts;
    //Обьекты
    public Slider Slider;//Слайдер к выпадению бонусса
 
    //Тексты вывода
    [Header("Текст, отвечающий за отображение очков")]
    public Text ScoreText;
    [Header("Текст, отвечающий за отображение бонуса нажатий в секунду")]
    public Text PerSecText;
    [Space]
    [Space]
    [Header("Магазин")]
    public List<Item> shopItems = new List<Item>();
    [Header("Текст цены на кнопках товаров")]
    public Text[] shopItemsCostText;
    [Header("Текст уровня на кнопках товаров")]
    public Text[] shopItemsLevelText;
 
    private void Start()
    {
        StartCoroutine(PerSecond());
    }
 
    private void FixedUpdate()
    {
            ScoreText.text = "" + score;//Вывод очков на экран
    }
    public void OnClick()
    {
        score += scoreIncrease;//
        Slider.value++;//Увеличение значения слайдера на 1
    }
 
 
    public void Buy_Item_CPS(int index)
    {
        score -= ItemCosts[index];
        LevelOfItem++;
        ItemCosts[index] *= 2.2f;
        shopItemsLevelText[index].text = "" + LevelOfItem;
        shopItemsCostText[index].text = "" + ItemCosts;
        StartCoroutine(PerSecond());
    }
 
 
    IEnumerator PerSecond(int index)
    {
        while (true) {
            score += LevelOfItem * perSecondBonus[index].value;//И вот здесь я затупил и не могу передать значения массива 
        yield return new WaitForSeconds(1);
            bonusPerSec = LevelOfItem * perSecondBonus[index].value;//и здесь
        }
    }
 
 
 
 
 
 
 
 
 
 
}
 
 
[Serializable]
public class Item
{
 
}

According to my idea, I specify in the junction for each object my own value of the multiplier (float) "bonusPerSec" and pass it to the coroutine, but I don't know how to implement it, because I just don't know the method of passing the array value to the coroutine - please help!
And in addition to this, at the start of the coroutine, you need to specify the index, how to do this, I, unfortunately, do not know either ...
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Daniil Basmanov, 2018-05-11
@Farset

Arrays are passed to coroutines in the same way as to all other methods:

using System.Collections;
using UnityEngine;

public class CoroutineExample : MonoBehaviour
{
    private void Start()
    {
        StartCoroutine(Example(new float[] {0, 1, 2, 3}));
    }

    private IEnumerator Example(float[] floats)
    {
        yield return null;
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question