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).
You’re mixing up a rendering material with a physics material. Different types with completely different purposes. Just like the material in your clothes.
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.
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
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.
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