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
$$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.
Your answer
Follow this Question
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