-1

I hope my question is not duplicate; I have a problem in unity. I write a class for transparency(fade in/fade out loop) of object.that's worked right. I put two button in scene.They are in canvas place. after run ,Even though I have written a command for transparency one object in canvas, all the things inside go to transparency(fade in and fade out loop). I do n't know. please help.

myclass

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

public class Transparent : MonoBehaviour {

private float duration =  .7f;
    public float waitTime;
    IEnumerator co2;
    // 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;
        Color textureColor = this.transform.GetComponent<Image>().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.transform.transform.GetComponent<Image> ().material.color = textureColor;

            // reset the timer

            yield return new WaitForSeconds (waitTime);



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

    }

    void stop_Transparency () 
    {

        this.StopCoroutine (this.co2);

    }
}

picture of my project

picture of my project

ali karimifard
  • 115
  • 2
  • 13

1 Answers1

0

Ok i found you problem it is mainly of

this.transform.transform.GetComponent<Image>();

Because of double transform, it reaches one upper level to parent which is your canvas itself. So everything in your canvas looses alpha value.

Also edited your code a bit. Instead of using materials color, directly changed Image Component's color value of alpha.

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

public class Transparent : MonoBehaviour
{
    private float duration = .7f;
    public float waitTime;
    IEnumerator co2;

    public void start_tranparecncy()
    {
        this.co2 = this.blink();
        this.StartCoroutine(this.co2);
    }

    IEnumerator blink()
    {
        //Color textureColor = this.transform.GetComponent<SpriteRenderer> ().material.color;
        Color textureColor = this.transform.GetComponent<Image>().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.transform.GetComponent<Image>().color = textureColor;

            // reset the timer

            yield return new WaitForSeconds(waitTime);  
        }
        //end of if(this.transform.childCount =0)

    }

    void stop_Transparency()
    {   
        this.StopCoroutine(this.co2); 
    }
}

I've attached this script to a button in canvas. Also run the start_tranparecncy() function in the same button's On Click().

After pressing the button, only the button's alpha value is changing.

You can change the On Click() and add on another button if you like.

derHugo
  • 83,094
  • 9
  • 75
  • 115
Thalthanas
  • 496
  • 6
  • 10
  • my friend i need to run fade in/fade out in start scene...i run start_transparency method with script.......for eg :throw_text_p1.gameObject.GetComponent ().start_tranparecncy (); command . – ali karimifard Sep 28 '17 at 13:44