Okay, I understand! You want to change the color of the InventorySlot itself when it's active or inactive, instead of its scale. You also want to expose these colors in the Inspector.
Here's how we can modify your InventorySlot script:
- Add Color Variables: Introduce two
public Color variables for the active and normal states.
- Get Renderer Reference: Get a reference to the
Renderer component on the InventorySlot GameObject in Awake(). This is what we'll use to change the color.
- Modify
SetActiveVisual: Change the transform.localScale line to modify the slotRenderer.material.color instead.
Here's the updated script:
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("Item Visual Settings")] // Renamed header for clarity
public Vector3 heldScale = new Vector3(0.5f, 0.5f, 0.5f);
public Vector3 heldRotation = new Vector3(10f, 180f, 0f);
[Header("Slot Visual Settings")] // New header for slot-specific visuals
public Color activeColor = Color.yellow; // Default active color
public Color normalColor = Color.white; // Default normal color
private GameObject storedItem;
private GameObject previewInstance;
private string originalObjectName;
private Vector3 originalScale;
private Renderer slotRenderer; // Reference to this slot's renderer
private void Awake()
{
// Get the Renderer component on this GameObject
slotRenderer = GetComponent<Renderer>();
if (slotRenderer == null)
{
Debug.LogWarning("InventorySlot: No Renderer found on this GameObject. Color changes will not be visible.", this);
}
}
public void SetActiveVisual(bool isActive)
{
// Change the slot's color based on its active state
if (slotRenderer != null)
{
slotRenderer.material.color = isActive ? activeColor : normalColor;
}
// Keep the stored item's visibility logic
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); // Item should be inactive when stored
CreatePreview(item);
gameObject.name = "Slot_" + originalObjectName; // Name the slot based on the item
}
void CreatePreview(GameObject item)
{
if (previewInstance != null)
Destroy(previewInstance);
// Instantiate the preview model
previewInstance = Instantiate(item, modelDisplayPoint.position, Quaternion.identity, modelDisplayPoint);
previewInstance.transform.localScale = Vector3.one * 0.3f; // Scale for display in UI
previewInstance.transform.rotation = Quaternion.Euler(heldRotation); // Use held rotation for preview
// Remove physics components from preview to prevent issues
DestroyImmediate(previewInstance.GetComponent<Rigidbody>());
DestroyImmediate(previewInstance.GetComponent<Collider>());
// Disable scripts on the preview instance if they exist, to prevent unintended behavior
MonoBehaviour[] scripts = previewInstance.GetComponents<MonoBehaviour>();
foreach (MonoBehaviour script in scripts)
{
script.enabled = false;
}
}
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 : transform.position; // Use slot's position if throwOrigin is null
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 != null ? throwOrigin.forward * 7f : transform.forward * 7f, ForceMode.Impulse); // Use slot's forward if throwOrigin is null
Destroy(previewInstance);
previewInstance = null;
storedItem = null;
gameObject.name = "Empty_Slot"; // Reset slot name
// Optionally, reset the slot color to normalColor here if it was active
if (slotRenderer != null)
{
slotRenderer.material.color = normalColor;
}
}
public GameObject GetStoredItem()
{
return storedItem;
}
}