C# list question.

My question is do all list have to be on the same script. I’m trying to make a card game and want to keep a master database list of all my cards then in another script take copies of cards from that list to another list but on that script it says there is nothing in my database list even though there is. If I have all my list in one script it works but when my list are in different scripts it doesn’t. I have also put my scripts in an executable order in the project settings but that didn’t fix it. I have been trying to figure this out for two days now and its driving me to just give up so if anyone can help me understand what I’m doing wrong that would be great.

edit (copied from “answer”)

here is an example of what I’m doing in my code.

Database script:

public list X = new List();

X.add (5);
Debug.Log (X[0]};

Other script:

Database Y = new Database();
List<int> Z = new List<int>();

Z.Add(Y.X[0]);
Debug.Log (Y.X[0]);
Debug.Log (Z{0]);

when I do this I get a “Out of range” error because it says there is nothing in the X list.
and if I do a debug.log using .count it says the right amount in the database script but 0 in the other script.

When you write:

    Database Y = new Database();

You create a new instance of your Database class, that’s what the “new” operator is good for. You still provided almost no information. Is your Database class derived from MonoBehaviour? If not, where and when is the list in this class populated? How does your other script get the reference to your Database instance?

You just provided code fragments which doesn’t explain anything in detail. From what we can see here you don’t have “one” Database. Again: You have to tell us what exactly you’re doing, otherwise we have to start guessing.

here is my scripts, also don’t I have to use the “new” keyword to make an instance so I can have access to my other scripts?

Card Class:

using UnityEngine;
using System.Collections;

public class CardClass
{
	public string _Name;//Re Ut
	public string _Color; //Ut
	public string _Race;
	public string _Rarity; //common, uncommon, rare Re Ut
	public int _OwnerID; //who currently controlls it Re Ut
	public int _CardID; //Match with other database Re Ut
	public int _TypeID; //Card type Re Ut
	public int _Class;//Ut
	public int _Cost1; //Lumber cost Ut
	public int _Cost2; //Ore Cost Ut
	public int _Cost3; //Crystal cost Ut
	public int _TextCost; //Re Ut
	public int _TextID; //0-13 Re Ut
	public int _Power;
	public int _Defense;
	public int _Range;
	public int _Speed;

	//Defualt
	public CardClass(){
		}

	//Unit, Ally, Structure Cards
	public CardClass(string Name, string color, string Race, string Rarity,
	                 int OwnerID, int CardID, int TypeID, int Class, int Cost1, int Cost2,
	                 int Cost3, int TextCost, int TextID, int Power, int Defense, int Range, int Speed){

			
				_Name = Name;
				_Color = color;
				_Race = Race;
				_Rarity = Rarity;
				_OwnerID = OwnerID;
				_CardID = CardID;
				_TypeID = TypeID;
				_Class = Class;
				_Cost1 = Cost1;
				_Cost2 = Cost2;
				_Cost3 = Cost3;
				_TextCost = TextCost;
				_TextID = TextID;
				_Power = Power;
				_Defense = Defense;
				_Range = Range;
				_Speed = Speed;

		}

		
		//Utility Cards
		public CardClass(string Name, string color, string Rarity, int OwnerID, int CardID, int TypeID, int Class, int Cost1, int Cost2, int Cost3, int TextCost, int TextID){

				_Name = Name;
				_Color = color;
				_Rarity = Rarity;
				_OwnerID = OwnerID;
				_CardID = CardID;
				_TypeID = TypeID;
				_Class = Class;
				_Cost1 = Cost1;
				_Cost2 = Cost2;
				_Cost3 = Cost3;
				_TextCost = TextCost;
				_TextID = TextID;
		}

		
		//Resource Cards
		public CardClass(string Name, string Rarity, int OwnerID, int CardID, int TypeID, int TextCost, int TextID){

				_Name = Name;
				_Rarity = Rarity;
				_OwnerID = OwnerID;
				_CardID = CardID;
				_TypeID = TypeID;
				_TextCost = TextCost;
				_TextID = TextID;
		}
	
}

Creating the cards:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class MainDataBase : CardClass {
	
	//public List<CardClass> MainDB = new List<CardClass> ();
	
				#region Unit
				// typeID = 1  Class: Meele = 1 Range = 2 Magic = 3   Rarity: Common, Uncommon, Rare, Omega  
				public CardClass Card0 = new CardClass ("Ember Berserker", "Red", "Dwarf", "Uncommon", 1, 0, 1, 1, 3, 0, 0, 0, 1, 3, 4, 1, 2);

				public CardClass Card1 = new CardClass ("Ember Lancer", "Red", "Dwarf", "Common", 1, 1, 1, 1, 1, 1, 0, 0, 0, 3, 3, 2, 2);

				public CardClass Card2 = new CardClass ("Ember Guardian", "Red", "Dwarf", "Uncommon", 1, 2, 1, 1, 2, 2, 0, 2, 2, 3, 5, 2, 1);

				public CardClass Card3 = new CardClass ("Ember Mage", "Red", "Dwarf", "Common", 1, 3, 1, 3, 0, 0, 3, 3, 3, 1, 2, 3, 3);

				public CardClass Card4 = new CardClass ("Ember Priest", "Red", "Dwarf", "Uncommon", 1, 4, 1, 3, 0, 0, 4, 3, 4, 1, 3, 4, 2);

				public CardClass Card5 = new CardClass ("Ember Hunter", "Red", "Dwarf", "Uncommon", 1, 5, 1, 2, 0, 2, 0, 0, 1, 2, 3, 4, 3);

				public CardClass Card6 = new CardClass ("Ember Gunner", "Red", "Dwarf", "Common", 1, 6, 1, 2, 1, 1, 0, 0, 0, 2, 2, 5, 3);
				#endregion

				#region Utility
				// typeID = 2  Class: Meele = 1 Range = 2 Magic = 3   Rarity: Common, Uncommon, Rare, Omega 
				public CardClass Card7 = new CardClass ("Cannon Tower", "Red", "Structure", "Uncommon", 1, 7, 2, 0, 2, 2, 0, 0, 5, 0, 4, 2, 0);

				public CardClass Card8 = new CardClass ("Mountain Lion", "Red", "Lion Ally", "Common", 1, 8, 2, 0, 0, 4, 0, 0, 0, 3, 3, 1, 3);

				public CardClass Card9 = new CardClass ("Lava Golem", "Red", "Golem Ally", "Rare", 1, 9, 2, 0, 0, 0, 6, 6, 6, 4, 4, 2, 3);

				public CardClass Card10 = new CardClass ("Burning Desire", "Red", "Rare", 1, 10, 2, 3, 0, 0, 4, 0, 7);

				public CardClass Card11 = new CardClass ("Hellfire", "Red", "Common", 1, 11, 2, 3, 0, 0, 3, 0, 8);

				public CardClass Card12 = new CardClass ("Healing Flames", "Red", "Common", 1, 12, 2, 3, 0, 0, 2, 0, 9);

				public CardClass Card13 = new CardClass ("Power Shot", "Red", "Common", 1, 13, 2, 2, 0, 3, 0, 0, 10);

				public CardClass Card14 = new CardClass ("Explosive Shot", "Red", "Uncommon", 1, 14, 2, 2, 0, 2, 2, 0, 11);

				public CardClass Card15 = new CardClass ("Flame Strike", "Red", "Common", 1, 15, 2, 1, 2, 0, 1, 0, 12);

				public CardClass Card16 = new CardClass ("War Cry", "Red", "Uncommon", 1, 16, 2, 1, 5, 0, 0, 0, 13);
				#endregion

				#region Resource
				// typeID = 3  Rarity: Common, Uncommon, Rare, Omega
				public CardClass Card17 = new CardClass ("Forest", "Common", 1, 17, 3, 0, 14);

				public CardClass Card18 = new CardClass ("Large Forest", "Uncommon", 1, 18, 3, 0, 15);

				public CardClass Card19 = new CardClass ("Quarry", "Common", 1, 19, 3, 0, 16);

				public CardClass Card20 = new CardClass ("Large Quarry", "Uncommon", 1, 20, 3, 0, 17);

				public CardClass Card21 = new CardClass ("Crystal Field", "Common", 1, 21, 3, 0, 18);

				public CardClass Card22 = new CardClass ("Large Crystal Field", "Uncommon", 1, 22, 3, 0, 19);
				#endregion

}

Adding the cards to a database:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class MDBADD : MonoBehaviour {
	
	public List<CardClass> MainDB = new List<CardClass> ();

	public void AddToDB(){

		DeckLists Deck = new DeckLists ();
		MainDataBase DB = new MainDataBase ();

		MainDB.Add (DB.Card0);
		MainDB.Add (DB.Card1);
		MainDB.Add (DB.Card2);
		MainDB.Add (DB.Card3);
		MainDB.Add (DB.Card4);
		MainDB.Add (DB.Card5);
		MainDB.Add (DB.Card6);
		MainDB.Add (DB.Card7);
		MainDB.Add (DB.Card8);
		MainDB.Add (DB.Card9);
		MainDB.Add (DB.Card10);
		MainDB.Add (DB.Card11);
		MainDB.Add (DB.Card12);
		MainDB.Add (DB.Card13);
		MainDB.Add (DB.Card14);
		MainDB.Add (DB.Card15);
		MainDB.Add (DB.Card16);
		MainDB.Add (DB.Card17);
		MainDB.Add (DB.Card18);
		MainDB.Add (DB.Card19);
		MainDB.Add (DB.Card20);
		MainDB.Add (DB.Card21);
		MainDB.Add (DB.Card22);
		Debug.Log (MainDB.Count);
}
}

this script makes a list that will be used to pull from the database and add to another list:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class DeckLists : CardClass {

	public List<int> Deck1 = new List<int> ();

	public List<CardClass> MDB = new List<CardClass> ();

	CardClass NewCard = new CardClass ();
	
	MDBADD TDB = new MDBADD ();

	public void DeckA(){ 
	
				Deck1.Add (0);//1
				Deck1.Add (0);//2
				Deck1.Add (0);//3
				Deck1.Add (1);//4
				Deck1.Add (1);//5
				Deck1.Add (1);//6
				Deck1.Add (2);//7
				Deck1.Add (2);//8
				Deck1.Add (3);//9
				Deck1.Add (3);//10
				Deck1.Add (3);//11
				Deck1.Add (4);//12
				Deck1.Add (4);//13
				Deck1.Add (4);//14
				Deck1.Add (5);//15
				Deck1.Add (5);//16
				Deck1.Add (5);//17
				Deck1.Add (6);//18
				Deck1.Add (6);//19
				Deck1.Add (6);//20
				Deck1.Add (7);//21
				Deck1.Add (7);//22
				Deck1.Add (8);//23
				Deck1.Add (9);//24
				Deck1.Add (10);//25
				Deck1.Add (11);//26
				Deck1.Add (11);//27
				Deck1.Add (11);//28
				Deck1.Add (12);//29
				Deck1.Add (12);//30
				Deck1.Add (12);//31
				Deck1.Add (13);//32
				Deck1.Add (13);//33
				Deck1.Add (14);//34
				Deck1.Add (14);//35
				Deck1.Add (15);//36
				Deck1.Add (15);//37
				Deck1.Add (15);//38
				Deck1.Add (16);//39
				Deck1.Add (16);//40
				Deck1.Add (17);//41
				Deck1.Add (17);//42
				Deck1.Add (17);//43
				Deck1.Add (17);//44
				Deck1.Add (18);//45
				Deck1.Add (18);//46
				Deck1.Add (18);//47
				Deck1.Add (19);//48
				Deck1.Add (19);//49
				Deck1.Add (19);//50
				Deck1.Add (19);//51
				Deck1.Add (20);//52
				Deck1.Add (20);//53
				Deck1.Add (20);//54
				Deck1.Add (21);//55
				Deck1.Add (21);//56
				Deck1.Add (21);//57
				Deck1.Add (21);//58
				Deck1.Add (22);//59
				Deck1.Add (22);//60	
				Deck1.Add (0);//60
				Debug.Log (Deck1.Count);

		}


}

this takes the two list and makes a new one:

sing UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Card : CardClass
{

	CardClass NewCard = new CardClass ();
	
	MDBADD TDB = new MDBADD ();

	DeckLists Deck1 = new DeckLists();

	public List<CardClass> MDB = new List<CardClass>();

	public void Change(){


		int g = 0;
		int x = Deck1.Deck1 [g];
		
		for (int i = 0; i < 60; i++) {

			MDB.Add (TDB.MainDB[0]);
			g++;
			x = Deck1.Deck1 [g];
			
		}
	}