Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DrLlamastein · May 09, 2017 at 08:23 PM · gameobjectprefabscript.class

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RobAnthem · May 10, 2017 at 05:14 AM 1
Share

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.
     }
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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. } }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

94 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why don't the Prefabs in my GameObject appear? 1 Answer

Creating entities with some variations among them 0 Answers

Do you tend to have scripts to mark prefabs 1 Answer

Reinitialize prefab 0 Answers

How to save/load references to components and prefabs 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges