OnCollision, Change color.

If my player collides with a cube, I want that cube to turn white.

Simple concept, can’t get my code to work. I am using a character controller, I’m not sure if that changes anything.

Note: My player is tagged as a Player. My code is placed on the cube, and I have changed the color to white in the inspector.

Any ideas?

Here is my code:

ChangeColor

using UnityEngine;

public class ChangeColor : MonoBehaviour
{
    public Color myColor;

    void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.tag == "Player")
        {
            gameObject.GetComponent<Renderer>().GetComponent<Material>().color = myColor;
        }
    }
}

I think what you want to do can be done by just replacing line 11 with

gameObject.GetComponent().material.color = myColor;

im not sure though, and not at home at the moment so i cant really try it out

Tried it. Didn’t do anything.

You want to use material.SetColor([propertyID],Color.white);
there are 2 versions of SetColor method one where the first argument is a string that is the name of the property and the 2nd is the hashID for that property. the hash version is far faster and thus can make a noticable difference if you are changing multiple object’s color every frame.

    public Color m_color = Color.white;
    public string m_PropertyName = "_Color";

    //cached fields
    private int m_propertyID;
    private Material m_targetMaterial;

    //used to auto-cache the Material and the propertyID
    public Material TargetMaterial
    {
        get
        {
            if(m_targetMaterial != null) return m_targetMaterial;

            var m_targetRenderer = GetComponent<Renderer>();
            if(m_targetRenderer==null) return null;

            m_objectMaterial = m_targetRenderer.materials[0];

            m_propertyID = Shader.PropertyToID(m_PropertyName);
            return m_targetMaterial;
        }
    }

    private void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.tag == "Player")
        {
            TargetMaterial.SetColor(m_propertyID,m_color);
        }
    }

So how do I implement this into my game? I tried using this code as is, and it didn’t work.

Sorry, I’m fairly new to coding.

I want you to answer every question no matter how obvious the answer may seem. This is purely for clarity sake.

  1. Is this a 2D or 3D game?
  2. Does your player collide with the cube(physically stopped) or trigger the cube(move through)?
  3. Does the cube have a rigidbody on it?
  4. What material do you have on the cube?
  1. 3D
  2. Player uses character controller, and it’s a cube. I have a OnTriggerEnter event that kills the player. (Because collision isn’t working with my character controller) So I’m not sure how to answer that.
  3. No rigid body
  4. Just a standard material. I created my own “ChangeColor” material to use.

I think the problem is that I’m using a character controller but I’m not sure how to fix it.

For Q2, I was asking specifically about this cube you wanted to change color, and if the cube was a trigger. Seeing how you mentioned how you used triggers for another task, I’d say you know the difference, and I believe I can rightly assume this cube physically stops the player.

Instead of trying so fervently to change the color, please confirm that the cube can currently indeed receive the collision event with the character controller player, using print statements.

If the cube can’t detect the collision, try adding a rigidbody to the cube and tick the “Is Kinematic” checkbox.

If it still can’t, I’d say you are better off learning how to use rigidbodies for your characters anyway. Unity’s character controller has major drawbacks regardless of its initial merits.

Well the cube is a platform that the player(Which is a cube as well for testing) will jump on . But currently, I’m using a separate cube for testing purposes instead of a platform.

The whole idea is to jump on a platform, and when you do, the platform changes colors.

I think with the character controller, it’s not allowing me to detect collision with another object unless I use “OnTrigger”. But it seems that trigger is very limited. I guess i’ll have to switch my script to use rigidbody instead.

there are specific conditions for when OnCollision and OnTrigger messages will fire. refer to the Collision Action Matrix at the bottom of this page in the Manual and check if your cube and player meet those conditions

Just in-case anyone searches this. I switched my character controller to use a rigid body and the script @JoshuaMcKenzie gave me worked!

Thanks for your help!

add private Material m_objectMaterial;