C
C#3y ago
Nos_E3ks

Trying to fade In and out Object

I've been stuck for awhile now, Been trying to figure out how to loop it and have it fade in and out.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class fadeobj : MonoBehaviour {

void Update() {
var animation = 1;

if (animation == 1)
{
StartCoroutine(FadeOutObject());
}

if (animation == 2)
{
StartCoroutine(FadeInObject());
}
}
public IEnumerator FadeOutObject() {
while (this.GetComponent<Renderer>().material.color.a > 0)
{

Color objectColor = this.GetComponent<Renderer>().material.color;
float fadeAmount = objectColor.a - ( 2 * Time.deltaTime);
objectColor = new Color(objectColor.r, objectColor.g, objectColor.b, fadeAmount);
this.GetComponent<Renderer>().material.color = objectColor;
var animation = 2;
yield return null;
}
}

public IEnumerator FadeInObject() {
while (this.GetComponent<Renderer>().material.color.a < 0 )
{

Color objectColor = this.GetComponent<Renderer>().material.color;
float fadeAmount = objectColor.a - ( -2 * Time.deltaTime);

objectColor = new Color(Random.value, Random.value, Random.value);
this.GetComponent<Renderer>().material.color = objectColor;
var animation = 1;
yield return null;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class fadeobj : MonoBehaviour {

void Update() {
var animation = 1;

if (animation == 1)
{
StartCoroutine(FadeOutObject());
}

if (animation == 2)
{
StartCoroutine(FadeInObject());
}
}
public IEnumerator FadeOutObject() {
while (this.GetComponent<Renderer>().material.color.a > 0)
{

Color objectColor = this.GetComponent<Renderer>().material.color;
float fadeAmount = objectColor.a - ( 2 * Time.deltaTime);
objectColor = new Color(objectColor.r, objectColor.g, objectColor.b, fadeAmount);
this.GetComponent<Renderer>().material.color = objectColor;
var animation = 2;
yield return null;
}
}

public IEnumerator FadeInObject() {
while (this.GetComponent<Renderer>().material.color.a < 0 )
{

Color objectColor = this.GetComponent<Renderer>().material.color;
float fadeAmount = objectColor.a - ( -2 * Time.deltaTime);

objectColor = new Color(Random.value, Random.value, Random.value);
this.GetComponent<Renderer>().material.color = objectColor;
var animation = 1;
yield return null;
}
}
}
10 Replies
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Nos_E3ks
Nos_E3ksOP3y ago
it works so far, Just trying to figure out how to get it to loop lol but thank you
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Nos_E3ks
Nos_E3ksOP3y ago
ah I see, understandable, thank you
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Nos_E3ks
Nos_E3ksOP3y ago
alright will do, I will let ya know how it turns out Well it works, time to figure out how to loop it
Buddy
Buddy3y ago
Do ABSOLUTELY NOT DO THIS
while (this.GetComponent<Renderer>().material.color.a > 0) { ... }
while (this.GetComponent<Renderer>().material.color.a > 0) { ... }
Save the component in a variable and reuse it GetComponent is way too expensive for that
Nos_E3ks
Nos_E3ksOP3y ago
alright So after a bit of testing I got a loop to work, now I want it to change colors Thank you everyone that has Helped me. I got it all working now
using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public enum FadeType
{
FadeIn,
FadeOut,
FadeInOut,
FadeOutIn
}

public class Fadeimg : MonoBehaviour
{
[Tooltip("The UI element you want to fade")]
[SerializeField]
private MaskableGraphic element;

[Tooltip("Fade type")]
[SerializeField]
private FadeType fadeType;

[Tooltip("Fade time")]
[SerializeField]
private float fadeTime = 1f;

[Tooltip("Loop wait time")]
[SerializeField]
private float WaitTime = 1f;

[Tooltip("Faded Time")]
[SerializeField]
private float FadedTime;

private Color color;

void Start()
{
color = element.color;
switch(fadeType)
{
case FadeType.FadeIn:
StartCoroutine(FadeIn());
break;
case FadeType.FadeOut:
StartCoroutine(FadeOut());
break;
case FadeType.FadeInOut:
StartCoroutine(FadeInOut());
break;
case FadeType.FadeOutIn:
StartCoroutine(FadeOutIn());
break;
}
}

private IEnumerator FadeOut()
{
for(float a = fadeTime; a >= 0; a -= Time.deltaTime)
{
element.color = new Color(color.r, color.g, color.b, a);
yield return null;
}
}

private IEnumerator FadeIn()
{
for(float a = 0; a <= fadeTime; a += Time.deltaTime)
{
element.color = new Color(color.r, color.g, color.b, a);
yield return null;
}
}

private IEnumerator FadeInOut()
{
StartCoroutine(FadeIn());
yield return new WaitForSeconds(fadeTime);
StartCoroutine(FadeOut());
}

private IEnumerator FadeOutIn()
{
StartCoroutine(FadeOut());
yield return new WaitForSeconds(fadeTime);
yield return new WaitForSeconds(FadedTime);
StartCoroutine(FadeIn());
yield return new WaitForSeconds(WaitTime);
StartCoroutine(FadeOutIn());
}
}

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

public enum FadeType
{
FadeIn,
FadeOut,
FadeInOut,
FadeOutIn
}

public class Fadeimg : MonoBehaviour
{
[Tooltip("The UI element you want to fade")]
[SerializeField]
private MaskableGraphic element;

[Tooltip("Fade type")]
[SerializeField]
private FadeType fadeType;

[Tooltip("Fade time")]
[SerializeField]
private float fadeTime = 1f;

[Tooltip("Loop wait time")]
[SerializeField]
private float WaitTime = 1f;

[Tooltip("Faded Time")]
[SerializeField]
private float FadedTime;

private Color color;

void Start()
{
color = element.color;
switch(fadeType)
{
case FadeType.FadeIn:
StartCoroutine(FadeIn());
break;
case FadeType.FadeOut:
StartCoroutine(FadeOut());
break;
case FadeType.FadeInOut:
StartCoroutine(FadeInOut());
break;
case FadeType.FadeOutIn:
StartCoroutine(FadeOutIn());
break;
}
}

private IEnumerator FadeOut()
{
for(float a = fadeTime; a >= 0; a -= Time.deltaTime)
{
element.color = new Color(color.r, color.g, color.b, a);
yield return null;
}
}

private IEnumerator FadeIn()
{
for(float a = 0; a <= fadeTime; a += Time.deltaTime)
{
element.color = new Color(color.r, color.g, color.b, a);
yield return null;
}
}

private IEnumerator FadeInOut()
{
StartCoroutine(FadeIn());
yield return new WaitForSeconds(fadeTime);
StartCoroutine(FadeOut());
}

private IEnumerator FadeOutIn()
{
StartCoroutine(FadeOut());
yield return new WaitForSeconds(fadeTime);
yield return new WaitForSeconds(FadedTime);
StartCoroutine(FadeIn());
yield return new WaitForSeconds(WaitTime);
StartCoroutine(FadeOutIn());
}
}

This is the Code I used to get it work
justaeris
justaeris3y ago
@Nos_E3ks don't forget to /close your post if you're done :D
Nos_E3ks
Nos_E3ksOP3y ago
alright thanks
Want results from more Discord servers?
Add your server