'using UnityEngine; public class InventorySlot : MonoBehaviour { [Header("References")]…
- User
'using UnityEngine;
public class InventorySlot : MonoBehaviour { [Header("References")] public Transform modelDisplayPoint; // For preview in inventory UI public Transform throwOrigin; // Where to throw object from public Transform holdPoint; // In front of camera for real object
[Header("Visual Settings")] public Vector3 heldScale = new Vector3(0.5f, 0.5f, 0.5f); public Vector3 heldRotation = new Vector3(10f, 180f, 0f); public Vector3 activeScale = new Vector3(1.1f, 1.1f, 1.1f); public Vector3 normalScale = Vector3.one; private GameObject storedItem; private GameObject previewInstance; private string originalObjectName; private Vector3 originalScale; public void SetActiveVisual(bool isActive) { transform.localScale = isActive ? activeScale : normalScale; if (storedItem != null) { storedItem.SetActive(isActive); } } public bool HasItem() { return storedItem != null; } public void StoreItem(GameObject item) { storedItem = item; originalObjectName = item.name; originalScale = item.transform.localScale; // Rename object to slot item.name = gameObject.name; // Smooth scale + rotate to hold item.transform.localScale = heldScale; item.transform.localRotation = Quaternion.Euler(heldRotation); // Move to front of camera (hold point) if (holdPoint != null) { item.transform.SetParent(holdPoint); item.transform.localPosition = new Vector3(0.3f, -0.2f, 0.6f); // tweak as needed } // Make kinematic if has Rigidbody Rigidbody rb = item.GetComponent<Rigidbody>(); if (rb != null) { rb.isKinematic = true; rb.useGravity = false; } storedItem.SetActive(false); CreatePreview(item); gameObject.name = "Slot_" + originalObjectName; } void CreatePreview(GameObject item) { if (previewInstance != null) Destroy(previewInstance); previewInstance = Instantiate(item, modelDisplayPoint.position, Quaternion.identity, modelDisplayPoint); previewInstance.transform.localScale = Vector3.one * 0.3f; previewInstance.transform.rotation = Quaternion.Euler(heldRotation); DestroyImmediate(previewInstance.GetComponent<Rigidbody>()); DestroyImmediate(previewInstance.GetComponent<Collider>()); } public void ThrowItem() { if (storedItem == null) return; // Restore original name & scale storedItem.name = originalObjectName; storedItem.transform.localScale = originalScale; // Unparent and show storedItem.SetActive(true); storedItem.transform.SetParent(null); storedItem.transform.position = throwOrigin != null ? throwOrigin.position : modelDisplayPoint.position; storedItem.transform.rotation = Quaternion.identity; Rigidbody rb = storedItem.GetComponent<Rigidbody>(); if (rb == null) rb = storedItem.AddComponent<Rigidbody>(); rb.isKinematic = false; rb.useGravity = true; rb.linearVelocity = Vector3.zero; rb.angularVelocity = Vector3.zero; rb.AddForce(throwOrigin.forward * 7f, ForceMode.Impulse); Destroy(previewInstance); previewInstance = null; storedItem = null; gameObject.name = "Empty_Slot"; } public GameObject GetStoredItem() { return storedItem; }} ' now i need instant of change scale on active inventory change color this color i give in public
- Assistant
Here's the modified script that changes color instead of scale when the slot is active:
using UnityEngine; using UnityEngine.UI; public class InventorySlot : MonoBehaviour { [Header("References")] public Transform modelDisplayPoint; // For preview in inventory UI public Transform throwOrigin; // Where to throw object from public Transform holdPoint; // In front of camera for real object [Header("Visual Settings")] public Vector3 heldScale = new Vector3(0.5f, 0.5f, 0.5f); public Vector3 heldRotation = new Vector3(10f, 180f, 0f); [Header("Color Settings")] public Color activeColor = new Color(0.2f, 1f, 0.2f, 1f); // Green by default public Color normalColor = Color.white; // Reference to the component that will change color private Image slotImage; // For UI Image private Renderer slotRenderer; // For 3D objects private GameObject storedItem; private GameObject previewInstance; private string originalObjectName; private Vector3 originalScale; void Awake() { // Try to get UI Image component slotImage = GetComponent<Image>(); // If no UI Image, try to get Renderer for 3D object if (slotImage == null) { slotRenderer = GetComponent<Renderer>(); } } public void SetActiveVisual(bool isActive) { // Change color based on active state Color targetColor = isActive ? activeColor : normalColor; if (slotImage != null) { slotImage.color = targetColor; } else if (slotRenderer != null) { slotRenderer.material.color = targetColor; } if (storedItem != null) { storedItem.SetActive(isActive); } } public bool HasItem() { return storedItem != null; } public void StoreItem(GameObject item) { storedItem = item; originalObjectName = item.name; originalScale = item.transform.localScale; // Rename object to slot item.name = gameObject.name; // Smooth scale + rotate to hold item.transform.localScale = heldScale; item.transform.localRotation = Quaternion.Euler(heldRotation); // Move to front of camera (hold point) if (holdPoint != null) { item.transform.SetParent(holdPoint); item.transform.localPosition = new Vector3(0.3f, -0.2f, 0.6f); // tweak as needed } // Make kinematic if has Rigidbody Rigidbody rb = item.GetComponent<Rigidbody>(); if (rb != null) { rb.isKinematic = true; rb.useGravity = false; } storedItem.SetActive(false); CreatePreview(item); gameObject.name = "Slot_" + originalObjectName; } void CreatePreview(GameObject item) { if (previewInstance != null) Destroy(previewInstance); previewInstance = Instantiate(item, modelDisplayPoint.position, Quaternion.identity, modelDisplayPoint); previewInstance.transform.localScale = Vector3.one * 0.3f; previewInstance.transform.rotation = Quaternion.Euler(heldRotation); DestroyImmediate(previewInstance.GetComponent<Rigidbody>()); DestroyImmediate(previewInstance.GetComponent<Collider>()); } public void ThrowItem() { if (storedItem == null) return; // Restore original name & scale storedItem.name = originalObjectName; storedItem.transform.localScale = originalScale; // Unparent and show storedItem.SetActive(true); storedItem.transform.SetParent(null); storedItem.transform.position = throwOrigin != null ? throwOrigin.position : modelDisplayPoint.position; storedItem.transform.rotation = Quaternion.identity; Rigidbody rb = storedItem.GetComponent<Rigidbody>(); if (rb == null) rb = storedItem.AddComponent<Rigidbody>(); rb.isKinematic = false; rb.useGravity = true; rb.linearVelocity = Vector3.zero; rb.angularVelocity = Vector3.zero; rb.AddForce(throwOrigin.forward * 7f, ForceMode.Impulse); Destroy(previewInstance); previewInstance = null; storedItem = null; gameObject.name = "Empty_Slot"; } public GameObject GetStoredItem() { return storedItem; } }Key changes made:
-
Added Color Settings:
activeColor- The color when the slot is active (green by default)normalColor- The color when the slot is inactive (white by default)
-
Component Detection:
- In
Awake(), the script automatically detects if it's attached to a UI Image or a 3D object with a Renderer
- In
-
Modified SetActiveVisual():
- Now changes color instead of scale
- Works with both UI Images and 3D objects
-
Removed Scale Variables:
- Removed
activeScaleandnormalScalesince they're no longer needed
- Removed
The script will automatically work whether your inventory slots are UI elements (using Image component) or 3D objects (using Renderer component). You can customize the colors in the inspector by changing the
activeColorandnormalColorvalues. -
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.