Class Container
Container interactable (chests, wardrobes, crates, etc.) that stores items and implements IItemContainer. Players can interact with it to view and take items. Configured via a ContainerTemplate ScriptableObject asset.
[RequireComponent(typeof(SceneObjectNamer))]
public class Container : Interactable, ISpawnable, IContainer, IInteractable, ISceneObject, IItemContainer
- Inheritance
-
ObjectComponentBehaviourMonoBehaviourNetworkBehaviourContainer
- Implements
- Inherited Members
-
NetworkBehaviour.IsSpawnedNetworkBehaviour.ComponentIndexNetworkBehaviour.NetworkObjectNetworkBehaviour.MAXIMUM_NETWORKBEHAVIOURSNetworkBehaviour.UNSET_NETWORKBEHAVIOUR_IDNetworkBehaviour.ToString()NetworkBehaviour.Reset()NetworkBehaviour.OnValidate()NetworkBehaviour.IsBehaviourReconcilingNetworkBehaviour.ClearReplicateCache()NetworkBehaviour.CreateReconcile()NetworkBehaviour.Reconcile_Reader<T>(PooledReader, ref T)NetworkBehaviour.OnStartServerCalledNetworkBehaviour.OnStartClientCalledNetworkBehaviour.OnStartNetwork()NetworkBehaviour.OnStopNetwork()NetworkBehaviour.OnStopServer()NetworkBehaviour.OnOwnershipServer(NetworkConnection)NetworkBehaviour.OnSpawnServer(NetworkConnection)NetworkBehaviour.OnDespawnServer(NetworkConnection)NetworkBehaviour.OnStartClient()NetworkBehaviour.OnStopClient()NetworkBehaviour.OnOwnershipClient(NetworkConnection)NetworkBehaviour.ClearBuffedRpcs()NetworkBehaviour.ExcludeOwnerFromUnbufferedObserversRpcsNetworkBehaviour.IsClientOnlyNetworkBehaviour.IsServerOnlyNetworkBehaviour.IsHostNetworkBehaviour.IsClientNetworkBehaviour.IsServerNetworkBehaviour.IsDeinitializingNetworkBehaviour.NetworkManagerNetworkBehaviour.ServerManagerNetworkBehaviour.ClientManagerNetworkBehaviour.ObserverManagerNetworkBehaviour.TransportManagerNetworkBehaviour.TimeManagerNetworkBehaviour.SceneManagerNetworkBehaviour.PredictionManagerNetworkBehaviour.RollbackManagerNetworkBehaviour.NetworkObserverNetworkBehaviour.IsClientInitializedNetworkBehaviour.IsClientStartedNetworkBehaviour.IsClientOnlyInitializedNetworkBehaviour.IsClientOnlyStartedNetworkBehaviour.IsServerInitializedNetworkBehaviour.IsServerStartedNetworkBehaviour.IsServerOnlyInitializedNetworkBehaviour.IsServerOnlyStartedNetworkBehaviour.IsHostInitializedNetworkBehaviour.IsHostStartedNetworkBehaviour.IsOfflineNetworkBehaviour.IsNetworkedNetworkBehaviour.GetIsNetworked()NetworkBehaviour.IsManagerReconcilingNetworkBehaviour.ObserversNetworkBehaviour.IsOwnerNetworkBehaviour.IsControllerNetworkBehaviour.HasAuthorityNetworkBehaviour.OwnerNetworkBehaviour.OwnerIdNetworkBehaviour.ObjectIdNetworkBehaviour.LocalConnectionNetworkBehaviour.OwnerMatches(NetworkConnection)NetworkBehaviour.Despawn(GameObject, DespawnType?)NetworkBehaviour.Despawn(NetworkObject, DespawnType?)NetworkBehaviour.Despawn(DespawnType?)NetworkBehaviour.Spawn(GameObject, NetworkConnection, Scene)NetworkBehaviour.Spawn(NetworkObject, NetworkConnection, Scene)NetworkBehaviour.RemoveOwnership()NetworkBehaviour.GiveOwnership(NetworkConnection)NetworkBehaviour.GetInstance<T>()NetworkBehaviour.TryRegisterInstance<T>(T)NetworkBehaviour.UnregisterInstance<T>()NetworkBehaviour.CanLog(LoggingType)MonoBehaviour.IsInvoking()MonoBehaviour.CancelInvoke()MonoBehaviour.StopCoroutine(Coroutine)MonoBehaviour.StopAllCoroutines()MonoBehaviour.destroyCancellationTokenMonoBehaviour.useGUILayoutMonoBehaviour.didStartMonoBehaviour.didAwakeMonoBehaviour.runInEditModeBehaviour.enabledBehaviour.isActiveAndEnabledComponent.GetComponent<T>()Component.TryGetComponent<T>(out T)Component.GetComponentInChildren<T>()Component.GetComponentsInChildren<T>()Component.GetComponentInParent<T>()Component.GetComponentsInParent<T>()Component.GetComponents<T>()Component.GetComponentIndex()Component.CompareTag(TagHandle)Component.transformComponent.transformHandleComponent.gameObjectComponent.tagObject.GetEntityId()Object.GetInstanceID()Object.GetHashCode()Object.InstantiateAsync<T>(T)Object.InstantiateAsync<T>(T, Transform)Object.InstantiateAsync<T>(T, Vector3, Quaternion)Object.InstantiateAsync<T>(T, Transform, Vector3, Quaternion)Object.Instantiate(Object, Vector3, Quaternion)Object.Instantiate(Object, Vector3, Quaternion, Transform)Object.Instantiate(Object)Object.Instantiate(Object, Scene)Object.Instantiate<T>(T, InstantiateParameters)Object.Instantiate<T>(T, Vector3, Quaternion, InstantiateParameters)Object.Instantiate(Object, Transform)Object.Instantiate<T>(T)Object.Instantiate<T>(T, Vector3, Quaternion)Object.Instantiate<T>(T, Vector3, Quaternion, Transform)Object.Instantiate<T>(T, Transform)Object.Destroy(Object)Object.DestroyImmediate(Object)Object.DontDestroyOnLoad(Object)Object.DestroyObject(Object)Object.FindObjectsOfType<T>()Object.FindObjectsByType<T>(FindObjectsSortMode)Object.FindObjectsByType<T>(FindObjectsInactive, FindObjectsSortMode)Object.FindObjectOfType<T>()Object.FindFirstObjectByType<T>()Object.FindAnyObjectByType<T>()Object.FindFirstObjectByType<T>(FindObjectsInactive)Object.FindAnyObjectByType<T>(FindObjectsInactive)Object.FindObjectsByType<T>()Object.FindObjectsByType<T>(FindObjectsInactive)Object.nameObject.hideFlags
Fields
AchievementTemplate
Achievement to increment when a player opens this container.
public AchievementTemplate AchievementTemplate
Field Value
Template
Template defining the container parameters.
public ContainerTemplate Template
Field Value
Properties
Items
Gets the list of items contained in this container.
public List<Item> Items { get; }
Property Value
Title
The display title for this interactable, shown in the UI.
public override string Title { get; }
Property Value
TitleColor
The color of the title displayed for this interactable in the UI.
public override Color TitleColor { get; }
Property Value
- Color
Methods
AddSlots(List<Item>, int)
Adds slots to the container, optionally initializing with a list of items.
public void AddSlots(List<Item> items, int amount)
Parameters
CanAddItem(Item)
Determines if the specified item can be added to the container.
public bool CanAddItem(Item item)
Parameters
itemItemThe item to check.
Returns
- bool
True if the item can be added, false otherwise.
CanInteract(IPlayerCharacter)
Returns true if the specified player character may interact with this object.
public override bool CanInteract(IPlayerCharacter character)
Parameters
characterIPlayerCharacterThe player character attempting to interact.
Returns
- bool
True if interaction is allowed, false otherwise.
Remarks
A pure question with no side effects. It used to stamp the character's interact rate-limit as part of answering, which made it unusable as a check: the scene server, the quest system and the client's input handler all call it, and each call silently consumed a limiter meant for one of the others. A quest accepted within the limiter's window of the interaction that opened the dialogue was refused by a debounce nobody intended to spend, and the refusal path sends nothing back — so the player saw a dead keypress. Spend the limiter explicitly with TryConsumeInteractRateLimit(IPlayerCharacter).
CanManipulate()
Determines if the container can be manipulated (e.g., items moved or swapped).
public bool CanManipulate()
Returns
- bool
True if manipulation is allowed, false otherwise.
Clear()
Clears all items from the container.
public void Clear()
ContainsItem(BaseItemTemplate)
Checks if the container contains an item with the specified template.
public bool ContainsItem(BaseItemTemplate itemTemplate)
Parameters
itemTemplateBaseItemTemplateThe item template to search for.
Returns
- bool
True if the item is found, false otherwise.
FilledSlots()
Gets the number of filled slots in the container.
public int FilledSlots()
Returns
- int
The number of filled slots.
FreeSlots()
Gets the number of free slots in the container.
public int FreeSlots()
Returns
- int
The number of free slots.
GetItemCount(BaseItemTemplate)
Gets the count of items matching the specified template.
public int GetItemCount(BaseItemTemplate itemTemplate)
Parameters
itemTemplateBaseItemTemplateThe item template to count.
Returns
- int
The number of items matching the template.
HasFreeSlot()
Checks if the container has at least one free slot.
public bool HasFreeSlot()
Returns
- bool
True if a free slot exists, false otherwise.
IsSlotEmpty(int)
Checks if the specified slot is empty.
public bool IsSlotEmpty(int slot)
Parameters
slotintThe slot index to check.
Returns
- bool
True if the slot is empty, false otherwise.
IsSlotLocked(int)
Returns true if the specified slot is currently locked. Locked slots cannot be swapped, removed, or transferred until unlocked.
public bool IsSlotLocked(int slot)
Parameters
slotintThe slot index to check.
Returns
- bool
True if the slot is locked, otherwise false.
IsValidSlot(int)
Checks if the specified slot index is valid for this container.
public bool IsValidSlot(int slot)
Parameters
slotintThe slot index to check.
Returns
- bool
True if the slot is valid, false otherwise.
LockSlot(int)
Locks the specified slot, preventing it from being swapped, removed, or transferred.
public void LockSlot(int slot)
Parameters
slotintThe slot index to lock.
OnAwake()
Called when the object is awakened. Override to implement custom initialization logic.
public override void OnAwake()
OnStartServer()
Rolls this container's contents. Server only, once per spawn.
public override void OnStartServer()
Remarks
OnStartServer() rather than OnAwake, for the same reason NPC ability
learning and scene object registration live there: Awake runs once per pooled instance,
so a container respawned from the pool would come back with whatever its previous life
left behind — which, since a looted chest ends empty, means empty forever.
ResetState(bool) clears and re-sizes the slots on the way into the pool and this
fills them on the way out.
The roll is server-side and the result reaches clients through WritePayload(NetworkConnection, Writer), which runs after this and before the spawn message is built. A client never rolls: it is told what is in the box.
ReadPayload(NetworkConnection, Reader)
Reads network payload data for this interactable, setting its ID and registering it in the scene.
public override void ReadPayload(NetworkConnection connection, Reader reader)
Parameters
connectionNetworkConnectionThe network connection.
readerReaderThe network reader.
RemoveItem(int)
Removes the item from the specified slot.
public Item RemoveItem(int slot)
Parameters
slotintThe slot index to remove the item from.
Returns
- Item
The item that was removed, or null if no item was present.
ResetState(bool)
Rebuilds the slot list when this instance returns to the pool.
public override void ResetState(bool asServer)
Parameters
asServerboolTrue when the reset is for the server instance.
Remarks
OnAwake() is what sizes the container, and Unity calls Awake once per instance rather than once per spawn — so a recycled container came back holding whatever its previous life had left in it, and a chest emptied by one player would respawn already empty for the next.
The slots are re-added rather than merely emptied, because a container whose template was assigned after Awake would otherwise come back with no slots at all.
SetItemSlot(Item, int)
Sets the item in the specified slot, refusing a locked slot. Callers must check the return value — see SetItemSlot(Item, int).
public bool SetItemSlot(Item item, int slot)
Parameters
Returns
SwapItemSlots(int, int)
Swaps items between two slots.
public bool SwapItemSlots(int from, int to)
Parameters
Returns
- bool
True if the swap was successful, false otherwise.
SwapItemSlots(int, int, out Item, out Item)
Swaps items between two slots and returns the items that were swapped.
public bool SwapItemSlots(int from, int to, out Item fromItem, out Item toItem)
Parameters
fromintThe source slot index.
tointThe destination slot index.
fromItemItemThe item originally in the source slot.
toItemItemThe item originally in the destination slot.
Returns
- bool
True if the swap was successful, false otherwise.
TryAddItem(Item, out List<Item>)
Attempts to add the specified item to the container, returning a list of modified items.
public bool TryAddItem(Item item, out List<Item> modifiedItems)
Parameters
itemItemThe item to add.
modifiedItemsList<Item>The list of items modified during the operation.
Returns
- bool
True if the item was successfully added, false otherwise.
TryGetItem(int, out Item)
Attempts to get the item in the specified slot.
public bool TryGetItem(int slot, out Item item)
Parameters
Returns
- bool
True if an item was found, false otherwise.
UnlockSlot(int)
Unlocks the specified slot, allowing normal manipulation again.
public void UnlockSlot(int slot)
Parameters
slotintThe slot index to unlock.
WritePayload(NetworkConnection, Writer)
Writes network payload data for this interactable, sending its ID to the writer.
public override void WritePayload(NetworkConnection connection, Writer writer)
Parameters
connectionNetworkConnectionThe network connection.
writerWriterThe network writer.
Events
OnSlotLockChanged
Event triggered when a slot's lock state changes. Parameters: container, slot index, isLocked.
public event Action<IItemContainer, int, bool> OnSlotLockChanged
Event Type
OnSlotUpdated
Event triggered when an item slot is updated (item added, removed, or changed).
public event Action<IItemContainer, Item, int> OnSlotUpdated