- Home /
How to bind different type of data in an array ?
How to declare different type of data in an array or list? I have written some code and i want to know about it is the right way to do this or not ? Any other method ? thanks in advance.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI;
<---------------------------------------------------- this is the main part.-----------------------------------> [System.Serializable] public class DataHandler { public int CoinsTarget; public int StepsTarget; public int TotalTarget; public List OtherData; } <-------------------------------------------------------------------------------------------------------------->
public class GameManager : MonoBehaviour { #region Variable Declarations
public List<DataHandler> Data; <--------------- this is the main part.
public static GameManager instance;
public int a= 0;
public bool b= false;
}
Answer by EXZ_EXZ · Jun 07, 2018 at 12:17 PM
you could do this by using a class or struct to store your data! just make a new class which contains everything you need and make a list/array out of it.
using UnityEngine;
public class DifferentDataList : MonoBehaviour
{
private DataStorage[] m_DataArray = new DataStorage[10];
private void Start()
{
m_DataArray[0] = new DataStorage("string", 1, 1.5f, Vector3.one);
Debug.Log(m_DataArray[0].m_String);
Debug.Log(m_DataArray[0].m_Int);
Debug.Log(m_DataArray[0].m_Float);
Debug.Log(m_DataArray[0].m_Vector3);
}
}
public class DataStorage
{
public string m_String;
public int m_Int;
public float m_Float;
public Vector3 m_Vector3;
public DataStorage(string _s, int _i, float _f, Vector3 _v)
{
m_String = _s;
m_Int = _i;
m_Float = _f;
m_Vector3 = _v;
}
}
Your answer
Follow this Question
Related Questions
Parent Class variables in Child Class's Inspector (C#) 0 Answers
Can I access variables of scripts that inherit from abstract classes? 2 Answers
How to access variable from other class? 1 Answer
public class variable in inspector like other components? 1 Answer
Access to a variable inside a C# class 2 Answers