Problem with color Change
what I'm trying to do is change my character's color once he touches an item and after 10 seconds i want it to change back to original color. thanks in advance.. here is my objects Script
using UnityEngine; using System.Collections;
public class ChalkaPickup : MonoBehaviour {
public Transform myTransform;
public Color changeToColor;
public Color baseColor;
public float changeDelay;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
changeColor ();
}
}
IEnumerator colorChange ()
{
yield return new WaitForSeconds (10f);
changeBack ();
}
void changeColor ()
{
Destroy (gameObject);
myTransform.GetComponent<Renderer>().material.color = changeToColor;
StartCoroutine (colorChange ());
}
void changeBack ()
{
myTransform.GetComponent<Renderer>().material.color =baseColor;
print ("Back");
}
}
Answer by UnityCoach · Dec 15, 2016 at 09:10 AM
You probably want to have the behaviour on your player object, not the pickup object.
You already have all the code you need, you just need to split it between the pickup object and the player object.
public class ChalkaPickup : MonoBehaviour
{
public Color changeToColor;
public float changeDelay;
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
other.StartCoroutine (other.GetComponent<ChalkaPlayer>().ChangeColor (changeToColor, changeDelay));
Destroy (gameObject);
}
}
}
public class ChalkaPlayer : MonoBehaviour
{
private Color baseColor;
private Renderer renderer;
void Awake ()
{
renderer = GetComponent<Renderer>();
}
public IEnumerator ChangeColor (Color color, float delay)
{
baseColor = renderer.material.color;
renderer.material.color = color;
yield return new WaitForSeconds (delay);
renderer.material.color =baseColor;
}
}
thanks for the help it looks a whole lot cleaner this way . but my issue is still there ... first off on the "other.StartCoroutine" the StartCoroutine part has an error so i tried getting rid of the "other" part and the error goes away but when he changes color he still doesn't change back after the time delay
hum, try other.GetComponent().StartCoroutine (...);
still doesn't accept the startCoroutine ..unless i don't anything in front of it
also after playing around with the code i notice it does change color and change back but only if i get rid of the Destroy(gameObject) on the pickUp Script maybe it should b placed somewhere else ..Sorry im a noob(1+ month ) so get trying to understand and get a hang of this
What we usually do is turn off collider, renderer and any other component that may be a problem like audiosource, then start the coroutine, which in turn, waits, does things, wait, does things, wait, and finally Destroy the gameObject.
other.GetComponent<ChalkaPlayer>().StartCoroutine (other.GetComponent<ChalkaPlayer>().ChangeColor (changeToColor, changeDelay));
Destroy (gameObject, delay);
Also, Destroy can be passed a delay argument, which saves you the trouble adding another coroutine.
THAN$$anonymous$$S it finally worked ...really appreciate it
Answer by Jason2014 · Dec 15, 2016 at 10:37 AM
I assume this is because you are using "Destroy" function. It "cancels" EVERY started coroutine. You have to insert this IEnumerator to player class. That should be solve a problem.
Your answer
Follow this Question
Related Questions
How to change player's color more than once 0 Answers
IENumerator does not work 0 Answers
Trying to add a delay to an Instantiate in a For Loop 0 Answers
[C#] Coroutine just won't stop! 2 Answers
"Can't add script behaviour AICharacterControl. The script needs to derive from MonoBehaviour!" ? 0 Answers