How do I make an action last for a few seconds? Unity c #

0

I need help in an action, that is to say that I have a collision function, what I want to do is that when the player object collides with that object that has the collision method my player turns red by the time that has entered the field of collision and then when it comes out of there it returns to its original color or material, is what I want to do, but I do not know how, I thank help in advance I have this code in collisions:

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

public class colisionvida : MonoBehaviour { int Dano = 0;

void OnTriggerEnter(Collider otro){
    Debug.Log ("Colisiona");
    otro.gameObject.SendMessageUpwards ("QuitarVida", Dano, SendMessageOptions.DontRequireReceiver);
}

}

And this in life:

void start(){gameObject.GetComponent().material = materialB;}

void QuitarVida (int Dano) { Vida -= Dano; gameObject.GetComponent().material = materialA; }

    
asked by Alien69 07.12.2017 в 03:03
source

1 answer

1

Well, I already solved it, what I did was to use the "yield return new WaitForSeconds (.06f);" Create a separate IEnumerator:


IEnumerator returne(){
        yield return new WaitForSeconds (.06f);
        GetComponent ().material.SetColor ("_Color", Color.white);
    }
Then use the void where you had the action to change the color:

void color(){
        GetComponent ().material.SetColor ("_Color", Color.red);
        StartCoroutine ("returne");
    }
And then in the void where you take your life call color ():

void QuitarVida (int Dano) {
        color ();
}

And ready

    
answered by 07.12.2017 / 17:40
source