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;
}
}
}
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.
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