Question by
stabilishero · Mar 01, 2020 at 12:35 AM ·
scripting problemlistclassesconvert
How to add Script1.List.Class to Script2.List.Class?
I am working on an item shop in which I have a public list shapesList of a class called Item_Shape, this class contains the values needed of each item for the store. These value's are in the store's script called Script_ShopMenu.cs. I have them added here so I can quickly add new shop items in the inspector.
public class Script_ShopMenu : MonoBehaviour
{
public Script_DataManager dataManager;
[System.Serializable]
public class Item_Shape
{
public string shapeName;
public float shapePrice;
public Sprite shapeSprite;
public bool shapeBought;
public bool shapeEquipped;
}
public GameObject shapeHolder, shapeHolderParent;
public List<Item_Shape> shapesList;
Now I make use of a saving system which goes through Script_DataManager.cs and it uses a script with all the variables I like to save called Script_PlayerData.cs
public class Script_PlayerData
{
//Variables to save
public float highScore = 0;
public float totalCoins = 0;
//Class of Item_Shape
[System.Serializable]
public class Item_Shape
{
public string shapeName;
public float shapePrice;
public Sprite shapeSprite;
public bool shapeBought;
public bool shapeEquipped;
}
//List of Item_Shapes
public List<Item_Shape> listOfShapes;
}
Error example
//Add items from list that are not in the second list
foreach (Item_Shape itemShape in shapesList)
{
if (!dataManager.data.listOfShapes.Contains(itemShape))
{
dataManager.data.listOfShapes.Add(itemShape);
}
}
Gives the error: Argument 1: cannot convert from 'Script_ShopMenu.Item_Shape' to 'Script_PlayerData.Item_Shape'
Problem: Now my issue is that I can not seem to convert the items from Script_ShopMenu.cs.shapesList to the save file script list Script_PlayerData.cs.listOfShapes. What I like to do is sort Script_ShopMenu.shapesList then compare this list with Script_Playerdata.listOfShapes and add missing items to Script_Playerdata.listOfShapes to save and load this in again.
Comment