Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by psychosaywhat · Oct 08, 2019 at 03:06 PM · scripting problemuiprefabs

prefab script accesses prefab values instead of instantiated values - but only in the player

I have a simple InventoryStore class that is attached to my player and to any storage crates in my game and a simple UI that comes up when a player opens a storage crate.

The UI has two "InventoryDetails" panels, one for the player, and one for the crate they've opened. The InventoryDetails panels are prefabs that have links to other prefabs for the inventory item's title, qty, a plus and minus button and a field for a qty to move. A script attached to the InventoryDetails panel creates rows of these title/qty/+/moveqty/- prefabs for each item in the subject's inventory. It also points the + and - buttons to the moveqty.

When the user presses an arrow button to either move inventory to the player or to the crate, the script on the InventoryDetailsPanel creates a "bundle" of the items to move and passes it to the player or to the crate, where it's applied and then the panels are refreshed to show updated quantites.

So... in the editor when I play it, everything works perfectly. When I build the project and run it, it doesn't work and is fetching values from the nested prefabs for the inventory quantity and the quantity to move. I feel like I'm missing something basic.

The InventoryDetails script, which instantiates the rows, has this code to set the buttons' targets:

 foreach (InventoryItemTypes i in subjectsInventory.Keys)
         {
             // instantiate the prefabs: 
             description  = Instantiate(itemDescriptionPrefab);
             qtyHeld      = Instantiate(itemQtyPrefab);
             plusButton   = Instantiate(plusBtnPrefab);
             moveQty      = Instantiate(moveQtyPrefab);
             minusButton  = Instantiate(minusBtnPrefab);
             ...
             // + (and -) button: 
             plusButton.transform.parent = this.transform;
             plusButton.GetComponent<MoveQtyButton>().inventoryValue = qtyHeld;
             plusButton.GetComponent<MoveQtyButton>().moveQty = moveQty;
         }

The + and - buttons have this script:

 public class MoveQtyButton : MonoBehaviour
 {
     public GameObject inventoryValue, moveQty;    
     public bool addOrSubtract = true; // default to add       
     public void OnClick()
     {
         if (float.TryParse(moveQty.GetComponent<TextMeshProUGUI>().text, out float currentMoveValue) && 
             float.TryParse(inventoryValue.GetComponent<TextMeshProUGUI>().text, out float currentInventoryValue))
         {
             //log
             if (addOrSubtract)
             {
                 if (Mathf.Round(currentInventoryValue) > 0)
                 {
                     currentMoveValue++;
                     currentInventoryValue--;
                 }
             } else
             {
                 if (Mathf.Round(currentMoveValue) > 0)
                 {
                     currentMoveValue--;
                     currentInventoryValue++; 
                 }
             }

... etc }

When I run the game in the player, the log shows that the scripts are finding the prefab's values, and not the values set in the panel. From the editor, everything finds the right values and the move works fine.

I'm wondering if anyone has seen this kind of thing or, after wading through all of the above, you have any advice?

thanks!

Jeff

Comment
Add comment · Show 1
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 tormentoarmagedoom · Oct 08, 2019 at 03:54 PM 0
Share

$$anonymous$$an so many code, long post... If pretend someone helps you, delete all unecessary code, and abstract the important information.

I will not read it.

normally whn people come here to ask this topic is:

because they change rpefab valuens like this:

 Instantiate (prefab, position, rotation)    
 prefab.SomeVariable = someavlue

Bur should do this:

 GameObject NewOject = Instantiate (prefab, position, rotation)    
 NewOject.SomeVariable = someavlue

Or because they declare something as static when it shouldnt be static.

0 Replies

· Add your reply
  • Sort: 

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

329 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 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 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 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 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 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 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 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 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 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 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 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 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

No Script option in component Menu?! (Unity 5.3.1) 3 Answers

Prefab UI Slider misses the fill and background items... 0 Answers

Move UI panel on and off screen, scripted without animation 0 Answers

Using downloaded image as UI Image's source image? 1 Answer

ObjectReference Not set to an instance of an object 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