Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by RafiXWPT · Aug 11, 2015 at 04:04 PM · uigetcomponentvariablesset

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.

Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JoshuaStrunk · Aug 11, 2015 at 04:48 PM 0
Share

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?)

avatar image RafiXWPT · Aug 11, 2015 at 04:51 PM 0
Share

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?

avatar image JoshuaStrunk · Aug 11, 2015 at 05:02 PM 0
Share

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.

avatar image JoshuaStrunk · Aug 11, 2015 at 05:19 PM 1
Share

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.

avatar image JoshuaStrunk · Aug 11, 2015 at 05:30 PM 1
Share

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

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# Access to a variable by another script attached in a gameObject in the scene. 2 Answers

Getting a Variable on Script B to equal a variable from script A 0 Answers

assigning my Canvas variable to the only Canvas I have in game 2 Answers

State machine with multiple player inputs? 1 Answer

Getting errors when referencing a variable. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges