Answer the question
In order to leave comments, you need to log in
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
{
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question