ali karimifard
ali karimifard

Reputation: 115

stop coroutine don't work in unity

I succeeded to fade in/fade out effect in unity.

But i don't stop this effect.

my class transparency of a game object :

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

public class Transparent_Of_Sprite : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }
    //----------------------------------------------------
    private float duration =  .7f;
    public float waitTime;
    IEnumerator co2;
    Color textureColor;
    // Update is called once per frame void 
    public void start_tranparecncy()
    {
        this.co2=this.blink();
        this.StartCoroutine (this.co2);
    }
    IEnumerator blink() { 

        //Color textureColor = this.transform.GetComponent<SpriteRenderer> ().material.color;
         textureColor = this.GetComponent<SpriteRenderer>().material.color;

        //textureColor.a = Mathf.PingPong(Time.time, duration) / duration; 
        //this.GetComponent<SpriteRenderer>().material.color = textureColor;
        while (true) { // this could also be a condition indicating "alive or dead"
            // we scale all axis, so they will have the same value, 
            // so we can work with a float instead of comparing vectors
            textureColor.a=Mathf.PingPong (Time.time, duration) / duration;
            this.GetComponent<SpriteRenderer> ().material.color = textureColor;

            // reset the timer

            yield return new WaitForSeconds (waitTime);



        }
        //end of if(this.transform.childCount =0)

    }

    public void stop_Transparency () 
    {
        textureColor.a = 5;
        this.GetComponent<SpriteRenderer> ().material.color = textureColor;
        this.StopCoroutine (this.co2);

    }
}

//------------------------- my code for stop transparency is ------------------------//

Game_Controller.Database [1, 1].Nute_M.GetComponent<Transparent_Of_Sprite> ().stop_Transparency ();

don't work for me.please help please

Upvotes: 0

Views: 512

Answers (1)

Daahrien
Daahrien

Reputation: 10320

Replace this:

this.co2=this.blink();
this.StartCoroutine (this.co2);

with this:

co2 = StartCoroutine(Blink());

You were starting the coroutine like a normal method.

EDIT:

Also replace IEnumerator co2; with Coroutine co2;

Upvotes: 1

Related Questions