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 Wach3D · Oct 14, 2016 at 12:33 AM · collisioncollidercolorcollision detectioncolor change

How to change the color of an object when it collides with another object?

I have a pong game and I'm trying to get the ball to change colors when it bounces off of the left and right paddles. Here's what I have so far:

 using UnityEngine;
 using System.Collections;
 
 public class CollisionColorChange : MonoBehaviour {
 
     public Color redcolor;
     public Color bluecolor;
 
 
     void OnCollisionEnter(Collision other) {
         if (other.transform.tag == "RedPaddle") 
         {            
             transform.GetComponent<Renderer> ().material.color = redcolor;
         }
 
         if (other.transform.tag == "BluePaddle") 
         {            
             transform.GetComponent<Renderer> ().material.color = bluecolor;
         }
     }
 }

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
2
Best Answer

Answer by Wach3D · Oct 18, 2016 at 06:29 PM

I figured it out! Took a while but it works!

So, my scene is indeed in 3d (to answer Mavina's question). I tried the example project, which really helped clarify how the color change could work. Although the method didn't work for me as my scripting is differently layed out.

Here's the new code:

 using UnityEngine;
 using System.Collections;
 
 public class CollisionColorChange : MonoBehaviour {
 
     public Color redcolor;
     public Color bluecolor;
 
 
     void OnTriggerEnter(Collider other) {
 
         if (other.gameObject.CompareTag ("RedPaddle")) 
         {           
             Debug.Log ("It's ALIVE and red");
             transform.GetComponent<Renderer>().material.color= redcolor;
         }
 
         if (other.gameObject.CompareTag ("BluePaddle")) 
         {            
             Debug.Log ("It's ALIVE and blue");
             transform.GetComponent<Renderer>().material.color= bluecolor;
         }
     }
 }

Both paddles were checked under their colliders as "Is Trigger". Then I added a Rigidbody to the Ball. I did all this because I decided to try OnCollisionEnter rather than OnTiggerEnter... I'm not sure why OnCollisionEnter didn't work, but at lease OnTriggerEnter worked.

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
avatar image
0

Answer by AsadShokat · Nov 01, 2019 at 05:45 AM

sir , i wanna to change the color of some other object not that on to which we trigger or collide how can i do so please guide me .its a great pleasure for me

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

103 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

Related Questions

how do I let a player pass through a wall when it is the same color? 0 Answers

Changing light color between players when they collide (BOLT) 0 Answers

How to change material color for certain time? 1 Answer

Collision With Text 0 Answers

[SOLVED]OverlapingSphere damage only one enemy 1 Answer


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