Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to copy a list of class from one script to another containing the same class within

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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;
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading