ScriptA
public class ScriptA: MonoBehaviour{
public List<LootItemsList> _lootItemsListToUse;
[System.Serializable]
public class LootItemsList
{
public GameObject _lootItem;
public Vector3 _lootItemOrigin;
public float _lootColliderActivationTime = 0.5f;
}}
ScriptB
public class ScriptB: MonoBehaviour{
public List<LootItemsList> _lootItemsListToUse;
[System.Serializable]
public class LootItemsList
{
public GameObject _lootItem;
public Vector3 _lootItemOrigin;
public float _lootColliderActivationTime = 0.5f;
}
void Start(){
ScriptA scriptA = ObjA.GetComponent<ScriptA>();
_lootItemsListToUse = scriptA._lootItemsListToUse;
}}
I get this error :
Error CS0029 Cannot implicitly convert type ‘System.Collections.Generic.List<ScriptB.LootItemsList>’ to ‘System.Collections.Generic.List<ScriptA.LootItemsList>’
I also tried this
_lootItemsListToUse = new List<ScriptA.LootItemsList>((IEnumerable<ScriptA.LootItemsList>)script._lootItemsList);
no error in MS studio but I get this error on play :
InvalidCastException: Specified cast is not valid.
(wrapper castclass) System.Object.__castclass_with_cache(object,intptr,intptr)
Please can someone help me with the correct way to copy this list? thanks
>Solution :
The most obvious answer would be: Don’t implement it twice. Rather reference it accordingly
ScriptA.cs
public class ScriptA: MonoBehaviour
{
public List<LootItemsList> _lootItemsListToUse;
[System.Serializable]
public class LootItemsList
{
public GameObject _lootItem;
public Vector3 _lootItemOrigin;
public float _lootColliderActivationTime = 0.5f;
}
}
ScriptB.cs
public class ScriptB: MonoBehaviour
{
public List<ScriptA.LootItemsList> _lootItemsListToUse;
void Start()
{
ScriptA scriptA = ObjA.GetComponent<ScriptA>();
_lootItemsListToUse = scriptA._lootItemsListToUse;
}
}
Or even: Don’t nest that class under another at all but have a third script in outer scope
LootItemList.cs
[System.Serializable]
public class LootItemsList
{
public GameObject _lootItem;
public Vector3 _lootItemOrigin;
public float _lootColliderActivationTime = 0.5f;
}
ScriptA.cs
public class ScriptA: MonoBehaviour
{
public List<LootItemsList> _lootItemsListToUse;
}
ScriptB.cs
public class ScriptB: MonoBehaviour
{
public List<LootItemsList> _lootItemsListToUse;
void Start()
{
ScriptA scriptA = ObjA.GetComponent<ScriptA>();
_lootItemsListToUse = scriptA._lootItemsListToUse;
}
}