Trying to save to a file, without calling each one individually

First, I would like to apologize if I get any terminology wrong.

Alright, I watched a quick tutorial on how to save information to a file and it looked fairly straight forward (BinaryFormatter/Serialize), but I am currently stumped on how to integrate it ‘dynamically’. By this I mean I am trying to use a variable (actionBarSlotNumber) to tell the saving part which slot it is saving for.

If I could get a variable to work in for example, contents.actionBarSlot(actionBarSlotNumber) = x, or something like that, I think it would do what I want, but I think I have reached the point where I know that it doesn’t work that way. That then leaves me without an idea of how to get actionBarSlot1 by using actionBarSlotNumber.

So, I have come to here to see if there is a way to do this or if I should be using a different method of saving information (that is not playerprefs).

Below is the current script I am working in, I assume I should move the saving portion to a separate script at some point, but for now I am just trying to get it to work.

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems; //needed for draging
using System; //needed for saving
using System.Runtime.Serialization.Formatters.Binary; //needed for saving
using System.IO; //needed for saving

public class DropArea : MonoBehaviour, IDropHandler, IPointerEnterHandler, IPointerExitHandler
{
	public bool isActionBarSlot = true;
	public int actionBarSlotNumber = 0;
	public float cdt;

	private DragScript.iconType _iconType;
	private GameObject _reppedPrefab;
	private Transform _containedIcon;

	void Update () 
	{
		if( cdt == 0 ) //sees if the value is zero
		{
			return; //should end the checking
		}
		else if( cdt > 0 ) //sees if the remaining time is above zero
		{
			cdt = cdt - Time.deltaTime; //reduces the remaining time until an ability can be used again
		}
		else if( cdt < 0 ) //makes sure the value isn't below zero
		{
			cdt = 0; //sets the value to zero
		}
		else //sanity check
		{
			Debug.LogError("Math Stopped Working!");
		}
	}

	public void OnDrop( PointerEventData eventData )
	{
		GameObject _oTD = eventData.pointerDrag.GetComponent<DragScript>().objectToDrag;

		Debug.Log(eventData.pointerDrag.name + " was dropped on " + gameObject.name);

		if( gameObject.transform.childCount >= 1 ) //checks to see if the current slot has and children
		{
			//will need a type check for gear
			if( _oTD.GetComponent<DragScript>().isClone == true ) //checks to see if the object being dragged is in clone state
			{
				for (int i = 0; i < gameObject.transform.childCount; i++) 
				{
					Destroy( gameObject.transform.GetChild(i).gameObject ); //destroys each child
				}
			}
			else if( _oTD.GetComponent<DragScript>().startingParent.GetComponent<DropArea>() != null )
			{
				for (int i = 0; i < gameObject.transform.childCount; i++) 
				{
					gameObject.transform.GetChild(i).gameObject.transform.SetParent( _oTD.GetComponent<DragScript>().startingParent.transform ); 
				}
			}
			else
			{
				Debug.Log("Unforseen Outcome");
			}
		}

		_oTD.transform.SetParent(gameObject.transform);
	}

	public void OnPointerEnter( PointerEventData eventData )
	{
		//Debug.Log("OnPointerEnter");
	}

	public void OnPointerExit( PointerEventData eventData )
	{
		//Debug.Log("OnPointerExit");
	}

	private void OnEnable()
	{
		if( isActionBarSlot == true ) 
		{
			Messenger.AddListener( "UseAbility" + actionBarSlotNumber, UseAbility ); //receives broadcast for abilitiy usage based on slot number and then calls the UseAbility function
		}
	}

	private void OnDisable()
	{
		if( isActionBarSlot == true ) 
		{
			Messenger.RemoveListener ( "UseAbility" + actionBarSlotNumber, UseAbility );
		}
	}

	private void UseAbility()
	{
		Debug.Log("I hear and obey... " + actionBarSlotNumber);

		int _childCount = gameObject.transform.childCount; //gets the number of children currently parented by the slot

		if ( _childCount < 1 ) //if there are no children, run nothing else
		{
			Debug.Log("Slot " + actionBarSlotNumber + " is empty");
			return;
		}

		GetIconDetails();

		if( _reppedPrefab == null ) //if there is no prefab repped, run nothing else
		{
			Debug.LogError("Slot " + actionBarSlotNumber + "'s icon is not tied to a prefab!");
			return;
		}

		if( _iconType == DragScript.iconType.SPELL || _containedIcon.GetComponent<DragScript>().itemHasUse == true )
		{
			if( PC.Instance.GetComponent<TabTarget>().selectedTarget != null ) //makes sure there is a target
			{
				if( cdt == 0 ) //if the cooldown is zero, moves on to the next check
				{
					int resource = (int)_reppedPrefab.GetComponent<Spell>().costType; //takes the vital type from the spell and converts it to an int for later use
					int cost = (int)_reppedPrefab.GetComponent<Spell>().baseCost; //takes the cost from the spell and converts it to an int for later use

					if( PC.Instance.GetVital(resource).CurValue - cost >= 0 ) //makes sure we have enough of a vital to use the ability
					{
						Debug.Log("Using Ability" + actionBarSlotNumber);
						PC.Instance.GetVital(resource).CurValue = PC.Instance.GetVital(resource).CurValue - cost; //removes the cost of the ability from its vital
						cdt = _reppedPrefab.GetComponent<Spell>().baseCooldown;
						Instantiate( _reppedPrefab, PC.Instance.transform.position, Quaternion.identity ); //creates a copy of the spell prefab
					}
					else //what happens if there is not enough of a vital to use an ability
					{
						Debug.Log("Not Enough " + (VitalName)resource); //should tell the player so
					}
				}
				else
				{
					Debug.Log(cdt + "Seconds Remaining"); //states remaining cooldown
				}

			}
			else
			{
				Debug.Log("No Target for Ability" + actionBarSlotNumber);
			}
		}

	}

	private void GetIconDetails()
	{
		_containedIcon = gameObject.transform.GetChild(0).transform; //gets a reference to what should be the only child of the slot
		_iconType = _containedIcon.GetComponent<DragScript>().typeOfIcon; //saves the icon type for later
		_reppedPrefab = _containedIcon.GetComponent<DragScript>().representedPrefab; //saves the repped prefab for later
	}

	public void SaveSlotContents()
	{
		BinaryFormatter bf = new BinaryFormatter();

		FileStream file = File.Open (Application.persistentDataPath + "/actionBarSlotDatabase.dat", FileMode.OpenOrCreate);

		int _childCount = gameObject.transform.childCount; //gets the number of children currently parented by the slot

		if (_childCount < 1) //
		{ 
			Debug.Log ("Slot is empty... clearing info");
		} 
		else 
		{
			GetIconDetails();
		}

		file.Close();
	}

	public void LoadSlotContents()
	{
		
	}
		
}


//Data that we want saved via this method needs to be of a 'clean' class or outside of MonoBehaviour
[Serializable] //states that this class can be saved to a file

public class ActionBarSlotContents
{
	public GameObject actionBarSlot1;
	public GameObject actionBarSlot2;
	public GameObject actionBarSlot3;
	public GameObject actionBarSlot4;
	public GameObject actionBarSlot5;
	public GameObject actionBarSlot6;
	public GameObject actionBarSlot7;
	public GameObject actionBarSlot8;
	public GameObject actionBarSlot9;
	public GameObject actionBarSlot10;
	public GameObject actionBarSlot11;
}

I would make the ActionBarSlotContents just have a List actionBarSlots.

List<GameObject> actionBarSlots =  new List<GameObject>();

This would let you use actionBarSlots[1] or actionBarSlots[3]. Allowing you to very easily grab a specific action bar slot. This would also let you have “infinite” action bar slots where as right now you have to manually add a new variable every time. Hope this helps.