IndexOutOfBounds and incorrect values

So, I’m writing a resource manager for my game, and… I’m trying to get it to add some resources, however. It’s not working correctly.

using UnityEngine;
using System.Collections;

public class ResourceManager : MonoBehaviour {

	public static int FOOD = 0, LUMBER = 1, STONE = 2, GOLD = 3;
	public int[] playerResource = new int[10];
	public static string[] RESOURCE_NAMES = {
		"Food",
		"Lumber",
		"Stone",
		"Gold"
	};
	
	// Use this for initialization
	void Start () {
		addResource (FOOD, 100);
		addResource (LUMBER, 100);
		addResource (STONE, 100);
		addResource (GOLD, 75);
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	void addResource(int id, int amm) {
		for(int i = 0; i < playerResource.Length; i++) {
			string resource = RESOURCE_NAMES*;*

_ if(playerResource == id) {_
* Debug.Log (“Adding: " +amm+ " of " +resource+” to stock"); *
* }*
* }*
* }*

* void deductResource() {*

* }*
}

It’s Debugging that it’s adding 100 to food, lumber, stone, AND GOLD, when it should only be adding 75 to gold, and then throwing an array out of bounds error, and I can’t for the life of me, figure out why.

As @Hoeloe pointed out, this is a pretty bad way to do things, which is why this is riddled with errors.

First off, you’re looking to see if playerResources *== id but player resources hasn’t been initialized, so it’s a miracle this does anything.*
Second, iterating over the whole array every time is equally bad.
Again, as people suggested a Dictionary is the best approach.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ResourceManager : MonoBehaviour {

//public static int FOOD = 0, LUMBER = 1, STONE = 2, GOLD = 3;
//public int[] playerResource = new int[10];
Dictionary<string, int> playerResources= new Dictionary<string, int>();

// Use this for initialization
void Start () {
addResource (“FOOD”, 100);
addResource (“LUMBER”, 100);
addResource (“STONE”, 100);
addResource (“GOLD”, 75);
}

// Update is called once per frame
void Update () {

}

void addResource(string resource, int amt) {
if (!playerResources.ContainsKey(resource)){
playerResources.Add(resource,0);
}
playerResources[resource] += amt;
}

void deductResource() {

}
}
OR if you want to keep up with the resources as an enum:
public class ResourceManager : MonoBehaviour {

enum ResourceType {Food, Lumber, Stone, Gold};
Dictionary<ResourceType, int> playerResources= new Dictionary<ResourceType, int>();

// Use this for initialization
void Start () {
addResource (ResourceType.Food, 100);
addResource (ResourceType.Lumber, 100);
addResource (ResourceType.Stone, 100);
addResource (ResourceType.Gold, 75);
}

void addResource(ResourceType resource, int amt) {
if (!playerResources.ContainsKey(resource)){
playerResources.Add(resource,0);
}
playerResources[resource] += amt;
}
}
T̶h̶e̶ ̶o̶n̶l̶y̶ ̶p̶o̶t̶e̶n̶t̶i̶a̶l̶ ̶d̶r̶a̶w̶b̶a̶c̶k̶ ̶w̶i̶t̶h̶ ̶e̶n̶u̶m̶ ̶i̶s̶ ̶t̶h̶a̶t̶ ̶y̶o̶u̶ ̶c̶a̶n̶’̶t̶ ̶u̶s̶e̶ ̶a̶n̶ ̶e̶n̶u̶m̶ ̶t̶y̶p̶e̶ ̶a̶s̶ ̶a̶ ̶d̶i̶c̶t̶i̶o̶n̶a̶r̶y̶ ̶k̶e̶y̶ ̶s̶o̶ ̶y̶o̶u̶ ̶h̶a̶v̶e̶ ̶t̶o̶ ̶c̶a̶s̶t̶ ̶i̶t̶ ̶t̶o̶ ̶i̶n̶t̶.̶