- Home /
Edit not active UI Panel Component
Hello, I am working on Inventory system. Now I implemented Equipment window and added "right click to wear" method. Everything works fine BUT! Only if Equipment panel is visible. When panel is not visible (`.SetActive(false)`) My method doesn't work. Now question. It is possible to edit variables in not active UI components? Or I have to rewrite my system to .alpha = 0f/1f
instead?
Thanks!!
Code of method:
public void WearItem(ContainerSlot from, CharacterEquipmentSlot to)
{
if(from != null && to != null)
{
if(from.Container.containerItems[from.id].ItemType == to.equipmentPart ||
((from.Container.containerItems[from.id].ItemType == ItemType.ONEHAND_WEAPON || from.Container.containerItems[from.id].ItemType == ItemType.TWOHAND_WEAPON)) && to.equipmentPart == ItemType.WEAPON)
{
if(from.Container.containerItems[from.id].ItemType == ItemType.TWOHAND_WEAPON)
{
CharacterEquipmentSlot shield = Array.Find(FindObjectsOfType<CharacterEquipmentSlot>(), slot => (slot.equipmentPart == ItemType.SHIELD));
if(shield.Item.ItemType == ItemType.NULL)
{
from.Container.containerItems[from.id] = to.Item;
to.Item = draggingItem;
characterEquipment.RecalculateStats();
this.HideToolTip();
}
else
{
from.Container.containerItems[from.id] = to.Item;
to.Item = draggingItem;
from.Container.AddItemToContainer(shield.Item.ItemId,1);
shield.Item = new Item();
characterEquipment.RecalculateStats();
this.HideToolTip();
}
}
else if (from.Container.containerItems[from.id].ItemType == ItemType.SHIELD)
{
CharacterEquipmentSlot mainHand = Array.Find(FindObjectsOfType<CharacterEquipmentSlot>(), slot => (slot.equipmentPart == ItemType.WEAPON));
if(mainHand.Item.ItemType == ItemType.NULL || mainHand.Item.ItemType == ItemType.ONEHAND_WEAPON)
{
from.Container.containerItems[from.id] = to.Item;
to.Item = draggingItem;
characterEquipment.RecalculateStats();
this.HideToolTip();
}
else
{
from.Container.containerItems[from.id] = to.Item;
to.Item = draggingItem;
from.Container.AddItemToContainer(mainHand.Item.ItemId,1);
mainHand.Item = new Item();
characterEquipment.RecalculateStats();
this.HideToolTip();
}
}
else
{
from.Container.containerItems[from.id] = to.Item;
to.Item = draggingItem;
characterEquipment.RecalculateStats();
this.HideToolTip();
}
}
}
else if (from != null && to == null)
{
CharacterEquipmentSlot eqSlot;
if(from.Container.containerItems[from.id].ItemType == ItemType.ONEHAND_WEAPON || from.Container.containerItems[from.id].ItemType == ItemType.TWOHAND_WEAPON)
{
eqSlot = Array.Find(FindObjectsOfType<CharacterEquipmentSlot>(), slot => (slot.equipmentPart == ItemType.WEAPON));
}
else
{
eqSlot = Array.Find(FindObjectsOfType<CharacterEquipmentSlot>(), slot => (slot.equipmentPart == from.Container.containerItems[from.id].ItemType));
}
this.WearItem(from, eqSlot);
return;
}
}
As you can see I need to get refference to CharacterEquipmentSlot "on the fly" cus "right click" on item runs that method with second parametr == null.
It is a little unclear what you mean by edit variables. Do you mean access public variables on a $$anonymous$$onoBehaviour attached to an inactive object. Or are you talking about having your "right click to wear" work when the panel is invisible? (if so is there another visual the user is clicking on?)
Thank you for your comment. I want to access public property in script called "Equipment" attached to EquipmentPanel. Unfortunatelly when EquipmentPanel is .SetActive(false) I can't do that. Is there any way to access it saving .SetActive(false) way to hide panels?
Where is the script trying to access the equipment variable located and how is it trying to access it? Scripts on inactive objects will not run on their own and it can be tricky to correctly find an object once it is inactive if you don't already have a reference to it. I just tested out the scenario in Unity 5.1 and as long as you have a reference to the Behavior you should be able to access public variables.
Is anything preventing you from storing a reference to CharacterEquipmentSlot in OnStart? It appears you only have one from your use of FindObjectsOfType. I would also recommend thinking about if this is the route you really want to take for finding objects at runtime. This would begin to break as soon as your scene has more than one character.
You can. The thing is you should really be asking "How do I get a reference to an inactive object?" I would take a look at this post on the subject http://answers.unity3d.com/questions/52560/gameobjectfind-work-on-inactive-objects.html
Answer by RafiXWPT · Aug 11, 2015 at 06:15 PM
@JoshuaStrunk solved my problem:
Where is the script trying to access the equipment variable located and how is it trying to access it? Scripts on inactive objects will not run on their own and it can be tricky to correctly find an object once it is inactive if you don't already have a reference to it. I just tested out the scenario in Unity 5.1 and as long as you have a reference to the Behavior you should be able to access public variables. Is anything preventing you from storing a reference to CharacterEquipmentSlot in OnStart? It appears you only have one from your use of FindObjectsOfType. I would also recommend thinking about if this is the route you really want to take for finding objects at runtime. This would begin to break as soon as your scene has more than one character.
Thank you.
Your answer
