changing material color with C#

i’m following this simple tutorial and it works fine, but i’d like to add 2 more cubes and 2 more color changes each time the falling cube hits the additional cubes:

i know the 2 new cubes must be tagged Box and 2 new materials must be created, but what is the script part? i tried something like this but it doesn’t work:

void OnCollisionEnter (Collision col)
{
if (col.gameObject.tag == “Box”)
{
rend.sharedMaterial = material [1];
rend.sharedMaterial = material [2];
}
else
{
rend.sharedMaterial = material [3];
}
}
}

thanks

gary

Hi Gary,

Try:

void OnCollisionEnter (Collision col){
            var otherRenderer = col.gameObject.GetComponent<Renderer>();

            if(otherRenderer != null){
                rend.material = otherRenderer.material;
            }
}

Place this on your falling cube and it should do what you are after, i think!

You shouldn’t need to do anything other than this, it will pick up a copy of the target colliders material as long as the Renderer component of the object also has a collider component attached to it.

If you want to only do this behaviour based on Tags you can introduce that, as this will do the same regardless if it collides with a box or the floor. Also I’m assuming you already have a reference to the falling cubes renderer in the variable ‘rend’.

Kind Regards,
Joe

going to need to know a little more detail about how you want that to work, what changes, when, based on what parameters? etc.

In the script you’ve provided the two colour changes happen in the same frame, there is no time interval between the two lines.

Simplest approach would be to just have a pointer which updates on each collision, so something like

int pointer = 0;

void OnCollisionEnter(Collision other)
{
    if(col.gameObject.tag == "Box"
    {
        rend.sharedMaterial = material[pointer];
        pointer = Mathf.Repeat(pointer + 1 , material.Length); // so the pointer doesn't excede the array length
    }
    else
    {
    // handle not hitting box
    }
}

here’s my entire code. your suggestion didn’t seem to work. the video is really quick if you have a minute to watch it you’ll see the process and code - thanks so much

public Material[ ] material;
Renderer rend;

void Start ()
{
rend = GetComponent ();
rend.enabled = true;
rend.sharedMaterial = material [0];
}

void OnCollisionEnter (Collision col)
{
if (col.gameObject.tag == “Box”)
{
rend.sharedMaterial = material[1];
}
else
{
rend.sharedMaterial = material [2];
}

}

}

Apologies, I’m at work and couldn’t watch the video nor test my code.

What LeftyRighty posted above should be the correct answer.

thanks. the video is only 5 min long and i was hoping to build on the code he created in that video.i can’t seem to get your code to work. i’m too new to this to jump around much between concepts. thanks though - appreciate it. he has it set up so 1 top cube falls down and bounces off another cube below it then hits the floor. i’d like to put 2 more cubes in that cascade so the top cube falls down and bounces off 3 cubes and then hits the floor, changing color upon each collision