iowaqas
iowaqas

Reputation: 161

Changing Cube's Color on Collision with Character Controller / C#

I have recently started to learn how to code and I am really enjoying it, I am little stuck and I'd like to have your help. I have added:

I would like the CUBE to change it's color if R key is pressed (which works) or if the player controller collides with the cube's collider.

Screenshot of my scene below

enter image description here

using UnityEngine;
using System.Collections;

public class colorChange : MonoBehaviour 
{
    public GameObject cube;
    private Renderer rend;

    // Use this for initialization
    void Start () 
    {

    }

    // Update is called once per frame
    void Update () 
    {
        // This will get the OBJECT to CHANGE COLOR on KEY PRESS
        if (Input.GetKeyDown (KeyCode.R))
            GetComponent<Renderer> ().material.color = Color.red;
        print ("A Key Pressed For Red Color");
    }

    void OnCollisionEnter (Collision col)
    {
        if (col.collider.name == "Matt")
        {
            rend.material.color = Color.yellow;
        }
    }
}

Here is a screenshot of the properties on the the two objects:

enter image description here

Upvotes: 4

Views: 5988

Answers (4)

iowaqas
iowaqas

Reputation: 161

Okay guys, I have managed to make it work. Below is my final code:

using UnityEngine;

using System.Collections;

public class colorChange : MonoBehaviour {

public Material color002;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

    // This will get the OBJECT to CHANGE COLOR on KEY PRESS
    if (Input.GetKeyDown (KeyCode.R)) {
        GetComponent<Renderer> ().material.color = Color.red;
        Debug.Log ("R Key Press For RED");

    // This will get the OBJECT to CHANGE MATERIAL on KEY PRESS
    } else if (Input.GetKeyDown (KeyCode.P)) {
        GetComponent<Renderer> ().material = color002;
        Debug.Log ("P Key Press For Pink Material (color002)");

    }
}

// This will get the OBJECT to CHANGE COLOR if the FPS Controller collides with Cube GameObject

void OnTriggerEnter(Collider other)

{
    print (other.name);
    if(other.name=="FPSController")

    {
        GetComponent<Renderer>().material.color = Color.green;
        Debug.Log ("FPS Collided with CUBE");
    }
}

}

Thanks a lot for your help! Time to keep on coding :D

Upvotes: 0

ziad antar
ziad antar

Reputation: 3

You need to use OnTriggerEnter instead of OnCollisionEnter Cause your cube not really solid (cause you enabled trigger boolean in the box collider)

void OnTriggerEnter(Collider other)
    {
        if(other.name=="Cube")
        {
            transform.parent.GetComponent<Renderer>().material.color = Color.red;
        }
    }

also try to use Debug.Log in a lot of places to make sure you are executing or not.

That might do it.

UPDATE: About the rend variable, since you have it private you need to assign it before executing the color changing, you'll get a NullReferenceException cause it's not assigned (the rend var) Either make it public and assign in the inspector or do it in the Start():

rend = GetComponent<Renderer>();

Upvotes: 0

Yupi
Yupi

Reputation: 4470

First if you are going to detect collision then your collider on cube shouldn't be trigger. If you are going to use it as a trigger then you should use OnTriggerEnter method. Anyway attach to the character controller another collider like sphere collider make sure it's not trigger and you are good. Use GetComponent() something like this:

void OnCollisionEnter(Collision col)
{
    if (col.gameObject.tag == "Player")
    {
        GetComponent<Renderer>().material.color = Color.yellow;
    }
}

Upvotes: 0

Serlite
Serlite

Reputation: 12258

The complication in your collision detection is that you're using a Character Controller, which doesn't exactly work within Unity's physics simulation. As a result OnCollisionEnter() will never be called during a collision between a Character Controller and a normal collider.

It sounds like what you need is the OnControllerColliderHit() method. As per the documentation:

OnControllerColliderHit is called when the controller hits a collider while performing a Move.

However, note that it's the Character Controller that receives the event, and not the object it bumped into. So if you do revise your code to use this, you'll need to put the event on the GameObject with the controller, and detect/change the cube's renderer colour from there:

void OnControllerColliderHit(ControllerColliderHit col) {
    if (col.collider.name == "cube")
    {
        col.gameObject.GetComponent<Renderer>().material.color = Color.yellow;
    }
}

Note: Because of all the physics-related headaches associated with using Unity's Character Controller, you may actually want to create your own version of it using a Rigidbody and a capsule collider. It will take a bit more scripting, but the solution Unity offers really doesn't work well with other parts of the engine.

Hope this helps! Let me know if you have any questions.

Upvotes: 2

Related Questions