Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

not work Coroutine

Discussion in 'Scripting' started by winter2010winter, Jul 2, 2019.

  1. winter2010winter

    winter2010winter

    Joined:
    Jul 2, 2019
    Posts:
    1
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class game : MonoBehaviour
    7. {
    8.     public GameObject[] balls;
    9.     public int random;
    10.     public void Ran()
    11.     {
    12.         random = Random.Range(0, max: 2);
    13.     }
    14.     // Use this for initialization
    15.     void Start()
    16.     {
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         if (Input.GetKeyDown("f"))
    23.         {
    24.             StartCoroutine("Fade");
    25.         }
    26.  
    27.         IEnumerator Fade()
    28.         {
    29.             Instantiate(balls[random], balls[random].transform.position, Quaternion.identity);
    30.         }
    31.     }
    32. }
    33.  
    34.  
    35.  
     

    Attached Files:

  2. chubbspet

    chubbspet

    Joined:
    Feb 18, 2010
    Posts:
    1,220
    Take your IEnumerator our of the Update function.
     
    adibichea likes this.
  3. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,745
    You declared corutine at method level, but calling it at class level. Do what chubbspet said or change how do you start coroutine to this

    Code (CSharp):
    1. StartCoroutine(Fade());