- Home /
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 :)
Resources are just a float value! (or a double, or an int, depending on how you want them to work) You can keep track of it in some 'GameEngine' class, and then query that from your GUI script to show them on the screen.
How much of your game have you done already? You can't just start with resources, have you got unit production in yet? I can't really design your game for you, presumably you have a method in $$anonymous$$d for resource gathering!
Well basically i have made the terrain made the soldier's follow my mouse and all i there is only one major thing left which is the Resources and i would like something like every 10 seconds it gives me 50 wood/Gold/Iron/Stone that sort of thing
Sorry, if you don't know such basic program$$anonymous$$g/scripting at all, you should give up on making games and get a programmer who does program$$anonymous$$g for you while you do the art or whatever else.
Answer by save · Oct 25, 2011 at 01:26 PM
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!
is this included in the other script? (resource$$anonymous$$anager.goldAmount += 50*resource$$anonymous$$anager.gold$$anonymous$$ines;)
also it came up with an error saying that it didn't know what resource$$anonymous$$anager means
You need to write your own resource$$anonymous$$anager. This stuff doesn't happen through magic, you know.
$$anonymous$$ake a script called resource$$anonymous$$anager and put it on a GameObject. ;-)
Answer by RayDaX · Aug 03, 2013 at 09:12 AM
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);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613021919im_/https://answers.unity.com/themes/thub/images/avi.jpg)