Resource Script

Hi i’am not very good at programming but i get there eventually but i can’t seem to find a script which does what i would like it to do

Basacilly i’am trying to make a strategy game and i’am stuck on how to generate resources and make it displayed on a GUI so any help would be appracited :slight_smile:

Your description is very vague as this can be done in a million ways. Make a plan for how this should be achieved, sketch it up on a mindmap where you can see the steps.

First you need to consider how resources are measured, does the player get more resources by owning land/mines/farms in quantity? Is the achievement of harvest done graphical so a lumberjack carries the wood into a house?

You'd then first need to make a system for each harvesting item, that could be as simple as a couple of int values. The player has 2 goldmines which means that every 10 seconds you'll receive 50*2 gold. You'll need to decide if this is done automatically or done by actual GameObject traveling from harvesting to central warehouse (like done in Warcraft 1/2). If so then you'd set up something like when the GameObject enters a certain area there's a trigger-function that adds onto an int which is displayed on the GUI.

Displaying something on the GUI is pretty straight forward,

static var goldMines : int = 1;
static var goldAmount : int = 0;

function OnGUI () {
    GUI.Label (Rect (10, 10, 60, 20), goldAmount.ToString());
}

So, set up a manager (empty GameObject with a script) which doesn't destroy on load if you have several levels where the player progress together with collected items. On that item make sure you have static variables which can be reached from another script.

resourceManager.goldAmount += 50*resourceManager.goldMines;

There's not so much else to it actually, good luck!

Here is a simple script i made to get resources calculated as a float value and in GUI it is added as a int to get rid of decimals.All you need to change is the Production amount / hour and updatetime for how often you want the data to update in seconds.

using UnityEngine;
using System.Collections;

public class PlayerData : MonoBehaviour {
	
	/*
	 * PlayerGold holds the gold amount in a float.
	 */
	public float PlayerGold = 0.0f;
	
	/*
	 * Gold production is calculated like this:
	 * 
	 * 100 Gold / Hour
	 * 
	 * Take 100 divided by 3600 seconds (3600 seconds = 1 hour) U get a value (0.0277777777777778)
	 */
	public float GoldProduction = 0.0277777777777778f;
	
	/*
	 * UpdateTime is how often you want your Gold value updated in seconds.
	 */
	public float UpdateTime = 10.0f; 
	
	
	
	// Use this for initialization
	void Start () {
		InvokeRepeating ("IncreaseResources", UpdateTime, UpdateTime);
		
	}
	
	void IncreaseResources() {
		PlayerGold += GoldProduction * UpdateTime;
		
	}
	
	void OnGUI(){
		GUI.Label(new Rect(100,100,100,25),"Gold: "+PlayerGold);
	
		
	}
}