I have the following problem with my app. I have a scene where the player is adding products with prices, by using strings. They are added to a Global Control file, which saves the data. Then, in this GlobalControl file, I want to add each of this strings in a list, so this way in the next scene I can call them and add them to a ScrollView, similar to order preview. Unfortunately, I can't manage to make it the right way and to add more than one item to the Order Preview.
My code is the following: Scene one:
public void addToOrder()
{
if (m_currentSelectedModel <= 33)
{
GlobalControl.orderContent =
menuItemNamesString[m_currentSelectedModel];
GlobalControl.priceContent =
menuItemPriceString[m_currentSelectedModel];
}
}
My GlobalControl is the following:
public class GlobalControl : MonoBehaviour
{
public static GlobalControl control;
//the global variables
static public string orderContent;
static public string priceContent;
//global lists
static public List<string> orderItemsList = new List <string>();
static public List<string> orderPricesList = new List <string>();
void Awake()
{
if (control == null)
{
DontDestroyOnLoad(gameObject);
control = this;
}
else if (control != null)
{
Destroy(gameObject);
}
}
private void Start()
{
//adding the current selection to the respective list
if (orderContent != null)
{
orderItemsList.Add(orderContent);
}
if (priceContent != null)
{
orderPricesList.Add(priceContent);
}
}
And last my OrderPreviewManager, which is in scene 2, is:
void Start ()
{
//my current solution, which does not retrieve a variable from a list
curOrderContent = GlobalControl.orderContent;
curPricetContent = GlobalControl.priceContent;
AddSelectedItemToOrder();
AddSelectedItemPriceToOrder();
}
//the two methods for adding the string to text components into a scroll
view
void AddSelectedItemToOrder()
{
GameObject gameObject = new GameObject("Child");
gameObject.transform.SetParent(orderContentView.transform);
gameObject.AddComponent<Text>().text = curOrderContent;
gameObject.GetComponent<Text>().font =
Resources.GetBuiltinResource(typeof(Font), "Arial.ttf") as Font;
gameObject.GetComponent<Text>().color = Color.black;
gameObject.GetComponent<Text>().fontSize = 20;
}
//similar to the previous method but for the prices
void AddSelectedItemPriceToOrder()
{
GameObject gameObject = new GameObject("Child");
gameObject.transform.SetParent(pricetContentView.transform);
gameObject.AddComponent<Text>().text = curPricetContent;
gameObject.GetComponent<Text>().font =
Resources.GetBuiltinResource(typeof(Font), "Arial.ttf") as Font;
gameObject.GetComponent<Text>().color = Color.black;
gameObject.GetComponent<Text>().fontSize = 20;
}
My question is: why does this only insert one item in scene 2?
static
from your class except for thestatic GlobalControl
. Let me know if it works, otherwise open another question at stackoverflow with Unity tag and I can perhaps help you debug it further there/show how I usually do it. – Fredrik Schön Dec 18 '18 at 16:52