Script example for unequipping an item on button press

Christ4673

New member
I've scoured the forums and the answer to that is always something complicated. I want to put the character in an unequipped state on button press, ideally id like to make it its own separate ability...could you give a start point for this? The current one (toggle equip) just toggles I want to unequip only.
 
/// ---------------------------------------------
/// Ultimate Character Controller
/// Copyright (c) Opsive. All Rights Reserved.
/// https://www.opsive.com
/// ---------------------------------------------

using UnityEngine;
using Opsive.UltimateCharacterController.Character;
using Opsive.UltimateCharacterController.Character.Abilities.Items;

public class BuildToggleWeaponOff : ItemAbility
{
[Tooltip("A reference to the Ultimate Character Controller character.")]
[SerializeField] protected GameObject m_Character;

/// <summary>
/// Equips the item.
/// </summary>
private void start ()
{
var characterLocomotion = m_Character.GetComponent<UltimateCharacterLocomotion>();
if (characterLocomotion != null) {
// Equip a specific index within the ItemSetManager with the EquipUnequip ability.
var equipUnequip = characterLocomotion.GetAbility<EquipUnequip>();
if (equipUnequip != null) {
// Equip the ItemSet at index -1 within the ItemSetManager.
equipUnequip.StartEquipUnequip(-1);
}
}
}
}
 
I try that and then put the ability to the highest priority in the item ability list, i dropped the character into the inspector...everything checks out, i get in my game and press the correct button but nothing happens. Ability shows active in the list but does not unequip the item...or seem to do anything. I'm positive it's probably something small that I'm missing. Any suggestions?
 
Maybe one of your existing abilities is preventing the unequip from occurring? Within StartEquipUnequip you can force the action to happen.

Beyond that I recommend that you step through the toggle item ability. If this ability works it should perform the same steps as what you want to do, except you don't want to save off the previously equipped index.
 
You were right thanks buddy! I can't believe it was so simple, there is one line all the way at the bottom, has these operators.

== and !=, i needed to change not equal to(!=) -1 to equal to(==) -1, cant believe all this time and all i needed to do was change a single character! it was this line:

// Equip the Next valid item if there is one.
if (m_NextItemSetIndex == -1) {
m_EquipUnequipItemAbility.StartEquipUnequip(m_NextItemSetIndex);

And with that small change now I have a strictly Unequip ability rather than a toggle. Thanks for all the help!
 
Top