1

I am developing an android game where I want to fade in the enemy as they spawn, The enemies are using standard (Specular shader), rendering mode is set it Fade ,prefabs have 0 alpha in albedo .To do this i use the following code

Enemy fadeIn

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

public class EnemySetAlpha : MonoBehaviour {

    public Color enemyMesh;
    public float fadetime = 255;

    // Use this for initialization
    void Start () {

        enemyMesh = GetComponent<SkinnedMeshRenderer> ().material.color;
    }

    // Update is called once per frame
    void Update () {
            fadeIn (fadetime);
    }

    void fadeIn(float time)
    {
        enemyMesh = new Color (1, 1, 1, 0 + (fadetime*Time.deltaTime));
    }
}

The problem I am facing is the the enemy that script increases the alpha but the albedo alpha is still 0 thus not fading in the enemy.

Ved Sarkar
  • 347
  • 2
  • 8
  • 18
  • Give it a shot to: `enemyMesh = GetComponent ().material; enemyMesh.SetColor(...);` – Alp Jan 15 '18 at 09:03

2 Answers2

4

Because Color is a struct (value type), not a class (reference type), changing your private color enemyMesh won't change the color of the material. enemyMesh = GetComponent<SkinnedMeshRenderer> ().material.color; makes a copy of the color of the material. A complete new entity.

You have to reference the material (which is a reference type) and change its color:

public class EnemySetAlpha : MonoBehaviour {

    public Material enemyMaterial;
    public float fadeSpeed = 0.1f;
    // Value used to know when the enemy has been spawned
    private float spawnTime ;

    // Use this for initialization
    void Start () {
        // Because `Material` is a class,
        // The following line does not create a copy of the material
        // But creates a reference (points to) the material of the renderer
        enemyMaterial = GetComponent<SkinnedMeshRenderer> ().material;
        spawnTime = Time.time ;
    }

    // Update is called once per frame
    void Update () {
            // Set the alpha according to the current time and the time the object has spawned
            SetAlpha( (Time.time - spawnTime) * fadeSpeed );
    }

    void SetAlpha(float alpha)
    {
        // Here you assign a color to the referenced material,
        // changing the color of your renderer
        Color color = enemyMaterial.color;
        color.a = Mathf.Clamp( alpha, 0, 1 );
        enemyMaterial.color = color;
    }
}
Hellium
  • 7,206
  • 2
  • 19
  • 49
  • Thank u this works, but how can a gradually increment fadetime to set alpha to 1, can you show it in your answer ? – Ved Sarkar Jan 15 '18 at 09:12
  • I've updated my answer to show how you can change the alpha value over time. – Hellium Jan 15 '18 at 09:18
  • This won't work as intended because `Time.time` returns overall time the game is running. So basically, this will set an alpha color greater than 1 whenever the game is running more than 10 seconds... – mrogal.ski Jan 15 '18 at 09:20
  • @m.rogalski : A simple Clamp and the problem is gone ;) – Hellium Jan 15 '18 at 09:23
  • @Hellium no, it is not. Assume OP will spawn enemy after 1 minute when the game was launched. Using your code enemy will spawn completely faded, opaque. It won't gradually fade out. – mrogal.ski Jan 15 '18 at 09:26
  • Yep, you are right. Fixed it. – Hellium Jan 15 '18 at 09:28
0

After your object is loaded you have to modify ( not just replace ) the alpha value by adding another "step" value.

Basically, what you need is this piece of code :

void setAlpha()
{
    Color c = material.color;
    float current = c.a;
    current = Mathf.Min(current + fadetime * Time.deltaTime, 1.0f);
    c.a = current;
    material.color = c;
}

Then in your Update call you can just make one simple check:

if(material.color.a < 1.0f)
{
    setAlpha();
}
mrogal.ski
  • 5,828
  • 1
  • 21
  • 30