Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by waittobi · Dec 15, 2016 at 08:30 AM · c#ienumeratorcolor change

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");

 }


     

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
 }
 }
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image waittobi · Dec 15, 2016 at 06:27 PM 0
Share

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

avatar image UnityCoach waittobi · Dec 15, 2016 at 07:24 PM 0
Share

hum, try other.GetComponent().StartCoroutine (...);

avatar image waittobi UnityCoach · Dec 15, 2016 at 07:51 PM 0
Share

still doesn't accept the startCoroutine ..unless i don't anything in front of it

avatar image waittobi · Dec 15, 2016 at 07:30 PM 0
Share

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

avatar image UnityCoach waittobi · Dec 15, 2016 at 09:02 PM 0
Share

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.

avatar image waittobi UnityCoach · Dec 15, 2016 at 09:26 PM 0
Share

THAN$$anonymous$$S it finally worked ...really appreciate it

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

278 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges