can if have a dynamic float in an array?

hey I’m trying for days to make a mining system with feedback in my GUI.

but i keep getting stuck in tracking the amount of mined Ore.

since I’m a wannabe coder I’m starting to wonder if i can do math or have a changing float in an array at one index? every thing is seem to try is resulting in an array getting stuffed with every step of my climbing number as a new object in the array or stays at 00…

i’ll post my scripts here but i don’t wanna scare people away with the amount to read for this fairly simple question :wink:

mining script;

#pragma strict
var oreAmount:float;
var miningSpeed:float;
var mined:float;
var minedOre:float;
var currentOre:float;
var type:String;
private var addType=false;
private var mining=false;
private var newType=false;
var mySkin:GUISkin;
var player:GameObject;
var inventoryScript:inventory;

function Start ()
{
	player=GameObject.FindGameObjectWithTag("Player");
	inventoryScript=player.GetComponent(inventory);
}

function OnMouseEnter () 
{	
	if(oreAmount>0)gameObject.renderer.material.SetColor("_Emission", Color.white);	
}
	
function OnMouseExit () 
{	
	if(oreAmount>0)gameObject.renderer.material.SetColor("_Emission", Color.black);
}

function OnCollisionEnter()
{
	type=tag;
	mining=true;
	addType=true;
}
function OnCollisionExit()
{
	mining=false;
	trackAmount=false;
}
function OnGUI ()
{   
	GUI.skin=mySkin;
	if(mining  oreAmount>0)
	{	
		addToInventory();
		var screenPos : Vector3 = Camera.main.WorldToScreenPoint (transform.position);
		GUI.Box (Rect (screenPos.x-5, screenPos.y-15, 65, 20), oreAmount.ToString());
	}
}
function addToInventory()
{	
	oreAmount -=mined;
	minedOre +=mined;
	if(inventoryScript.types.length==0)
	{
		newType=true;
	}
	inventoryScript.totalOre=(currentOre+minedOre).ToString("#00");
	for (var i=0;i< inventoryScript.types.length;i++)
	{
		if (inventoryScript.types[i]==type)
		{
			print("same");
			currentOre=inventoryScript.typeAmount[i];
			return;
		}
	 	if (inventoryScript.types[i]!=type)
		{
			newType=true;
		}
	}
	if (newType)
	{
		print("been in newType");
		inventoryScript.types.Add(type);
		inventoryScript.typeAmount.Add(minedOre);
		inventoryScript.amounts.Add(inventoryScript.totalOre);
		newType=false;
	}
}
function Update ()
{	
	print(inventoryScript.typeAmount.length);
	mined=Time.deltaTime*miningSpeed;
	if(oreAmount==0)
	{
		mining=false;
		trackAmount=false;
	}
}

inventory script;

var totalOre:String;
public var types= new Array();
public var typeAmount= new Array();
public var amounts= new Array();
var windowRect: Rect;
private var inventoryWinHeight: int;
private var inventoryWinWidth: int;
private var oreTypeWinWidth:int;
private var DisInventory=false;

function InventoryWindow ()
{	
	GUILayout.BeginArea(Rect (10,20,50,windowRect.height-50));
	for (var name:String in types)
		{
			GUILayout.TextArea(name);
		}
	GUILayout.EndArea();
	GUILayout.BeginArea(Rect (60,20,30,windowRect.height-50));
	
	for(var amountNR in amounts)
	{
		GUILayout.TextArea(amountNR);
	}
	GUILayout.EndArea();
	GUILayout.Space(windowRect.height-50);
	if (GUILayout.Button("close Inventory"))DisInventory=false;
}
function OnGUI ()
{
	if(!DisInventory)
	{
		if(GUI.Button(Rect(Screen.width-90, Screen.height -35, 75, 20), "Inventory"))DisInventory=true;
	}
	if(DisInventory)
	{	
		inventoryWinWidth = Screen.width/2;
		inventoryWinHeight = Screen.height/2; 
		oreTypeWinWidth = inventoryWinWidth/6;
		windowRect= new Rect ((Screen.width/2)-(inventoryWinWidth/2),(Screen.height/2)-(inventoryWinHeight/2),inventoryWinWidth,inventoryWinHeight);
		windowRect=GUILayout.Window (0, windowRect, InventoryWindow, "Inventory");
	}
print(amounts);
}

I think your problem lies here:

You iterate throug every element in inventoryScript.types and whenever the the type does not match you create a new one. But what you want is to only create a new entry if no entry is type already. Try something like this:

function addToInventory()

{   
    oreAmount -=mined;
    minedOre +=mined;
    if(inventoryScript.types.length==0)
    {
        newType=true;
    } else {
      newType = false;
   }
    inventoryScript.totalOre=(currentOre+minedOre).ToString("#00");
    for (var i=0;i< inventoryScript.types.length;i++)
    {
        if (inventoryScript.types[i]==type)
        {
            newType = true;
            print("same");
            inventoryScript.typeAmount[i] += minedOre; //if i got the idea of the variable right.
            currentOre=inventoryScript.typeAmount[i];
            return;
        }
    }
    if (newType)
    {
        print("been in newType");
        inventoryScript.types.Add(type);
        inventoryScript.typeAmount.Add(minedOre);
        inventoryScript.amounts.Add(inventoryScript.totalOre);
        newType=false;
    }
}

can i use indexOf and then do a calculation and put in a new array, or is there a better way
EDIT: oh i scrolled down a bit to fast didn’t see your answer their. thanks lockstep will try ASAP

hey i get the following error on that line
Operator ‘+’ cannot be used with a left hand side of type ‘Object’ and a right hand side of type ‘float’.
the array definitely is an array of floats… so does that mean i can use math on an element in an array?

Ah sorry i posted the syntax for the builtin array. They are faster but you can not rescale then which would be bad in your situation since you want to add new elements on the fly. You find more information on the different arrays in the docs.

I didn’t test this but i think it could work like this:
instead of

inventoryScript.typeAmount[i] += minedOre;

try

inventoryScript.typeAmount[i] = inventoryScript.typeAmount[i] + minedOre;

The reason for the above line not working is, that an array can hold any object so the compiler has troubles with linking a general object with a float. If you use a builtin array you would define it like this:

var someFloats : float[];

Which means the array only contains float and the compiler knows there are only floats in it thus it understands the += comand.

thanks.
i noticed it worked when changing ToBuiltin
will try that line you suggest also because i had to create an extra array now… if it’s possible without I’m all for