error CS0029: Cannot implicitly convert type 'UnityEngine.Material' to 'UnityEngine.PhysicMaterial'

Hi,

I am trying to set the alpha of a material via this piece of code (it’s a fall guys type of hexagon falling object):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FallPlat : MonoBehaviour
{
    public float fallTime = 0.5f;
    private Vector3 ObjPosition;
    Collider colliderFall;
    Material mat;

    private void Start()
    {
        ObjPosition = transform.position;
        colliderFall = GetComponent<Collider>();
        mat = GetComponent<Renderer>().material;
    }

    void OnCollisionEnter(Collision collision)
    {
        foreach (ContactPoint contact in collision.contacts)
        {
            //Debug.DrawRay(contact.point, contact.normal, Color.white);
            if (collision.gameObject.tag == "Player")
            {
                StartCoroutine(Fall(fallTime));
            }
        }
    }

    IEnumerator Fall(float time)
    {
        yield return new WaitForSeconds(time);
        Color color = mat.color;
        color.a = 0;
        mat.color = color;
        colliderFall.material = mat;
        colliderFall.enabled = false;
        transform.position = ObjPosition;
        yield return new WaitForSeconds(2);
        color.a = 255;
        mat.color = color;
        colliderFall.enabled = true;
    }
}

But I get the error code:
Assets\Obstacles\Scripts\FallPlat.cs(37,27): error CS0029: Cannot implicitly convert type 'UnityEngine.Material' to 'UnityEngine.PhysicMaterial'

When I change Material mat; to PhysicsMaterial.mat; it says that physics material does not contain colour (which it doesn’t - I know that).

Please help,
Thanks

The material of a collider is a different kind of material to what you think it is: Unity - Scripting API: PhysicMaterial

Has nothing to do with the materials that a renderer uses.

1 Like

So how would I reference the rendering material?

Same way you got the material originally.

1 Like

Just Material mat;? because I tried that and it doesn’t work

Unfortunately many years ago, it was misspelled but TBH, all you have to do is look at the scripting reference to see how it’s spelled etc. :slight_smile:

As previously linked: https://docs.unity3d.com/ScriptReference/PhysicMaterial.html

You’re mixing up a rendering material with a physics material. Different types with completely different purposes. Just like the material in your clothes. :wink:

I would suggest using an IDE which would likely type this for you or at least give you hints on it. It also seems like you need to follow some tutorials on basic rendering with materials and physics with physics materials.

1 Like

Just to clear up some points you seem to be confused about. Make sure you understand what

A Renderer is a component that adds a behaviour to a gameobject that is responsible to “render” / visually display this object in the scene.

A Collider is a component that is part of the physics system and gives the object a physical shape which is used for collision detection when the object is part of a moving rigidbody or just a stationary piece of geometry you could collide with

A Material is a set of parameters as well as a shader that is used by a Renderer. The shader and the parameters change / dictate how the object is rendered by the Renderer.

A PhysicMaterial is a set of parameters which set certain physical properties of a Collider. Namely friction and bounciness coefficients which are taken into account when a rigidbody collision occurs.

So your problem now is that this line just makes no sense:
colliderFall.material = mat;

Your colliderFall variable is a Collider. It does have a material property which takes a PhysicMaterial instance that, as just explained, change how the collider behaves under collisions. Your “mat” on the other hand is a (rendering) Material that could change how an object is rendered (drawn) in the scene when applied to a Renderer. However that’s not what you did. Please note that you have not specified what you actually wanted to achieve. Given that you change the color property of the material you probably wanted to change the visual appearence of the object. Though in that case your usage of “colliderFall” just makes no sense. When you’re painting and want to reach for the paint, you don’t grab into the box filled with nails.

So when you want to change the material of the renderer, you probably should set the material of the renderer :slight_smile:


ps: Note that Unity has two separate physics system (and recently even more). The classical 3d physics system (Rigidbody, Coliider, PhysicMaterial, Physics, …) and the 2d physics system (Rigidbody2D, Collider2D, PhysicsMaterial2D, Physics2D, …). Those are two separate systems and do not interact in any way.

3 Likes

Thanks. I’ll watch some tutorials. Just got back into C# after a few years break and now its fixed. Thanks for your help and sorry for bothering you with this stupid question. Also, I use Visual Studio and it didn’t come up with the suggestios it just told me the error :frowning:

1 Like

Thanks. It’s fixed now. Silly me :frowning:

1 Like

It’s not a bother at all, it’s what the forums are for.

Good luck.

1 Like

Thanks. I’m sure i’ll be on here sooner or later again for another issue

1 Like

That’s a GREAT post @Bunny83 … here’s my version:

Terminology primer for assets:

https://discussions.unity.com/t/901952/2

And also…

Unity’s component architecture

https://discussions.unity.com/t/808134/6

1 Like