- Home /
Resource specific class with values
I have a game where the world is randomly generated and you can find resources and when I mine them I want to get information about that resource, which is a prefab / game object. Stuff like the amount it will drop, the name etc. How would I do this? Would I do something like this for the prefab?
public class Resource {
public string resourceName;
public int resourceGainAmount;
}
Because ultimately I want each of my resource to have a script attached to them that holds special values like the name and the amount it will drop that I can then access from a different script that handles the mining of the resources.
I have no clue how classes work though that's why I'm not really sure what to do.
Albeit your current design idea would likely work. I'm not sure where you are getting stuck, so I will do my best to point you in the right direction. Personally I would use an enum because it is a safer method and easier to call out to such things, so for example you would have an enum like this.
public enum ResourceType
{
Wood,
Stone,
Ore
}
Then you would have your actual Resource class
public class Resource
{
public ResourceType resource;
public int $$anonymous$$inResource;
public int $$anonymous$$axResource;
public int GetResourceAmount()
{
return Random.Range($$anonymous$$inResource, $$anonymous$$axResource + 1);
}
}
Then more than likely you would want some sort of In-game representation of the resource.
public class ResourceObject : $$anonymous$$onoBehaviour
{
public Resource resource;
//other resource data
public void AllocateResource()
{
//Code to give player resource.
//Call this method from whatever decides that a player obtained X resource, whether from a collision, a raycast, a click event, whatever.
}
}
Answer by DrLlamastein · May 10, 2017 at 04:51 PM
As @RobAnthem said this is the solution.
Albeit your current design idea would likely work. I'm not sure where you are getting stuck, so I will do my best to point you in the right direction. Personally I would use an enum because it is a safer method and easier to call out to such things, so for example you would have an enum like this.
public enum ResourceType { Wood, Stone, Ore }
Then you would have your actual Resource class
public class Resource { public ResourceType resource; public int MinResource; public int MaxResource; public int GetResourceAmount() { return Random.Range(MinResource, MaxResource + 1); } }
Then more than likely you would want some sort of In-game representation of the resource.
public class ResourceObject : MonoBehaviour { public Resource resource; //other resource data public void AllocateResource() { //Code to give player resource. //Call this method from whatever decides that a player obtained X resource, whether from a collision, a raycast, a click event, whatever. } }