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

Using Coroutines to Fade in objects!

Discussion in 'Scripting' started by SP-Designs, Jun 10, 2016.

  1. SP-Designs

    SP-Designs

    Joined:
    Oct 13, 2015
    Posts:
    184
    Hey there.I have a script in which when the item that the script is attached to gets clicked, it de-activates and activates 2 other items.So i wanted the floatingisland item, to fade in slowly and i wrote a code with a coroutine inside it to make the island fadein.However it does not work and i guess it is because the floating island prefab consists of a ton of other items and as a whole it does not have a general color material.So i was thinking that maybe i could edit its mesh renderer to make it fade.I don't really know how to do this though so please help me! Here is my code :

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class TreasureItemsHandler : MonoBehaviour {
    6.  
    7.   public GameObject floatingisland;
    8.   public GameObject bridge;
    9.   public GameObject removablecollider;
    10.   Renderer islandrend;
    11.  
    12.   void Start()
    13.   {
    14.   islandrend = floatingisland.GetComponent<Renderer>();
    15.   }
    16.  
    17.   // Disabling the treasureitem when clicked!
    18.   void OnMouseDown()
    19.   {
    20.   gameObject.SetActive(false);
    21.   floatingisland.SetActive(true);
    22.   bridge.SetActive(true);
    23.   removablecollider.SetActive(false);
    24.   }
    25.  
    26.   void Update()
    27.   {
    28.   if (gameObject.activeSelf == false)
    29.   {
    30.   StartCoroutine("Fade");
    31.   }
    32.   }
    33.  
    34.   IEnumerator Fade()
    35.   {
    36.   for (float f = 1f; f >= 0; f += 0.1f)
    37.   {
    38.   Color c = islandrend.material.color;
    39.   c.a = f;
    40.   islandrend.material.color = c;
    41.   yield return new WaitForSeconds(.5f);
    42.   }
    43.   }
    44. }
    45.  
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    if the current gameobject isn't active the script isn't being run...

    Depending on how these gameobjects are being created and handled over time you might want to look at OnEnable, or move the coroutine call to Start()
     
  3. SP-Designs

    SP-Designs

    Joined:
    Oct 13, 2015
    Posts:
    184
    So if i plce the call to the coroutine at the void Start() {} it will work? xD I will try it
     
  4. jaasso

    jaasso

    Joined:
    Jun 19, 2013
    Posts:
    64
    this will fade the materials of the object set in the inspector and its children from transparent to opaque when you click an object with this script attached, maybe you can use it as reference to add the other stuff you needed, shouldnt be difficult:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class FadeTest : MonoBehaviour
    5. {
    6.     public GameObject floatingisland;
    7.  
    8.     Material[] mats;
    9.  
    10.     void Start()
    11.     {
    12.         Renderer[] r=floatingisland.GetComponentsInChildren<Renderer>();
    13.         mats=new Material[r.Length];
    14.         for(int i=0;i<r.Length;i++)
    15.         {
    16.             mats[i]=r[i].material;
    17.         }
    18.     }
    19.  
    20.     void OnMouseDown()
    21.     {
    22.         floatingisland.SetActive(true);
    23.         StartCoroutine("FadeIn",mats);
    24.     }
    25.  
    26.     public float fadeSpeed=.003f;
    27.     float lerpAmount;
    28.  
    29.     IEnumerator FadeIn(Material[] materialsToFadeIn)
    30.     {
    31.         lerpAmount=0;
    32.  
    33.         while(lerpAmount<1)
    34.         {
    35.             for(int i=0;i<materialsToFadeIn.Length;i++)
    36.             {
    37.                 Color c=materialsToFadeIn[i].color;
    38.                 c.a=Mathf.Lerp(0,1,lerpAmount);
    39.                 materialsToFadeIn[i].color=c;
    40.                 lerpAmount+=fadeSpeed;
    41.             }
    42.             yield return null;
    43.         }
    44.  
    45.         //gameObject.SetActive(false);
    46.     }
    47. }
     
  5. SP-Designs

    SP-Designs

    Joined:
    Oct 13, 2015
    Posts:
    184
    The Renderer[] r = floatingisland.getcomponentinchildrer<renderer>(); gives me an error that an array cannot be converted to a renderer :p !
     
  6. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,727
    Should'n your loop start at 0, instead of 1 if you are fading in?
    And the loop should go until f >= 1
    for (float f = 0f; f <= 1; f += 0.1f)

    The way the code is now the loop would run forever and never do anything useful
     
    Last edited: Jun 12, 2016
  7. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    Your missing a s
    GetComponentsInChildren not GetComponentInChildren