Modify the following Unity code so that the targetMaterial does not become an Instance…
- User
Modify the following Unity code so that the targetMaterial does not become an Instance when modifying its color. This is for a Color Picker system by the way. Here it is: "using UnityEngine; using System.Collections;
public class example : MonoBehaviour { public GameObject ColorPickedPrefab; public GameObject targetObject; // The GameObject that will receive the new color public int materialIndex = 0; // Index of the material to change private ColorPickerTriangle CP; private bool isPaint = false; private GameObject colorPickerInstance; private Material targetMaterial; private Renderer targetRenderer;
codevoid Start() { // Ensure a target object is set if (targetObject == null) { targetObject = gameObject; } // Get the renderer from the target object targetRenderer = targetObject.GetComponent<Renderer>(); if (targetRenderer == null) { Debug.LogError("Target object does not have a Renderer component."); return; } // Ensure the material index is valid if (materialIndex < 0 || materialIndex >= targetRenderer.materials.Length) { Debug.LogWarning("Invalid material index. Using the first material."); materialIndex = 0; } // Don't get the specified material from the target object. // This was done first, but causes to lose the brake light functions from the car controller. // Now the car body paint will be saved after playmode, but at least the brake light emmission will work again. // Original paint for reference is 220, 220, 220. targetMaterial = targetRenderer.materials[materialIndex]; } void Update() { if (isPaint && targetMaterial != null) { targetMaterial.color = CP.TheColor; } } void OnMouseDown() { if (isPaint) { StopPaint(); } else { StartPaint(); } } private void StartPaint() { if (targetMaterial == null) { Debug.LogError("No valid material found on the target object."); return; } colorPickerInstance = Instantiate(ColorPickedPrefab, transform.position + Vector3.up * 1.4f, Quaternion.identity); colorPickerInstance.transform.localScale = Vector3.one * 1.3f; colorPickerInstance.transform.LookAt(Camera.main.transform); CP = colorPickerInstance.GetComponent<ColorPickerTriangle>(); CP.SetNewColor(targetMaterial.color); isPaint = true; } private void StopPaint() { Destroy(colorPickerInstance); isPaint = false; } - Assistant