Pickup colour
On the same GameObject as an ST Pickup. The holder owns the object, so each press of the use button can change the synced colour, holder name and use count.
PickupColor.cs
using StageTime.SDK;
using TMPro;
using UnityEngine;
/// <summary>
/// On an STPickup: the use button (trigger / right mouse) cycles a synced colour applied to the renderer's material;
/// picking up and dropping update a synced "held by" label. The holder owns the object, so it may write synced values.
/// </summary>
public class PickupColor : StageTimeBehaviour
{
public TMP_Text label;
public Color[] colors;
[Synced] private Color color = Color.white;
[Synced] private string holder = "nobody";
[Synced] private int uses;
private Renderer meshRenderer;
private void Start()
{
meshRenderer = GetComponent<Renderer>();
Apply();
}
public override void OnPickup()
{
holder = Networking.LocalPlayer.displayName;
Apply();
}
public override void OnDrop()
{
holder = "nobody";
Apply();
}
public override void OnPickupUseDown()
{
uses++;
if (colors != null && colors.Length > 0) color = colors[uses % colors.Length];
Apply();
}
public override void OnPickupUseUp()
{
Apply();
}
public override void OnDeserialization()
{
Apply();
}
private void Apply()
{
if (meshRenderer != null) meshRenderer.material.color = color;
if (label != null) label.text = $"Held by: {holder}\nUses: {uses}";
}
}