- Home /
Copy collider material on trigger
HI, I would like to start by pointing out that I am not a complete newbie to programming, but I am only learning C# for the second day. I programmed my first simple game (snake :) ) in Turbo Pascal in high school, and one (maze) in unity years ago using JS. Now I'm creating a virtual showroom using SteamVR and need to use interactive elements. One of them is a color palette made up of a mesh object. I'm using custom sphere to pick color from each part of the palette (19 colors in total). The idea is that the sphere touches a material on the palette and that material is immediately displayed on the desired object (in my case lift cladding). I understand roughly how C# works, but I am unable to effectively track down the correct functions. I am using HDRP. Script should be attached to the sphere, but I was also considering attaching it to palette meshes. So far I ended up with this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeCladdingMaterial : MonoBehaviour
{
public GameObject liftCladding;
public Material material;
Renderer rend;
void Start()
{
rend = GetComponent<Renderer>();
rend.enabled = true;
rend.sharedMaterial = material;
}
void OnTriggerEnter(Collider coll)
{
if (coll.gameObject.tag == "Palette")
{
// first I was thinking something like this could work:
// rend = liftCladding.material._BaseColor = ???;
// or something like this:
// liftCladding.material.SetColor("_Color", ???);
}
}
Thanks for anybodys help by pointing me into the right direction.
Can you clarify how you are using / planning to use the materials:
Do you want to change the target renderers existing instance of material color or set new material with color according to the palette?
Also (if you want to set new material) do you have same material with individual instances that have individual colors (created via script) in the palette or do you use multiple materials in palette that have predefined color?
You said you that the Palette is made of mesh object. Do you mean one mesh and if so are all the materials on this same mesh?
Thank you for your reply @CodesCove ! I want to set color from palette to replace base color of existing material on the cladding. The palette consists of several meshes, each of which has its own color - it can also be a material that is applied to the target object. But Id like to use the easiest possible way.
Cladding material (maps will be added and should remain the same for every color):
Answer by CodesCove · Dec 07, 2021 at 02:47 PM
I think you have good start in your code.. Here is the simple example
public static MeshRenderer selectedTargetRenderer; //You can define this static variable also elsewhere. Just remeber to set the reference to the selected object
private void OnTriggerEnter(Collider palletteCollider)
{
if (palletteCollider.CompareTag("Palette")) this.CopyMaterialColorFromPalette(palletteCollider.GetComponent<MeshRenderer>());
}
private void CopyMaterialColorFromPalette(MeshRenderer selectedPalletteRenderer)
{
if (selectedPalletteRenderer != null && selectedTargetRenderer != null) selectedTargetRenderer.material.color = selectedPalletteRenderer.material.color;
}
Attach above code (or similar) to the selector object.
As commented in the code I would store the selected object / renderer to a static variable so it's easily accessible from everywhere.
Notice that material.color refers to the main color of the material.
Your answer
Follow this Question
Related Questions
Material Change Color on trigger. 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Trigger not working 2D 1 Answer
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer