Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Niboruga · Aug 13, 2017 at 02:26 PM · objectsclassesobject referenceclass objectclass instance

How to create a repository of enemy objects that are initialised but then get a new instance of a random one?

I am attempting to create a really simple turn based rpg. I have created an enemy object as follows:

 public class Enemy()
 {
     int attack;
     int hp;
     string name;
 
     public Enemy(string _name, int _hp, int _attack)
     {
         name = _name;
         hp = _hp;
         attack = _attack;
     }
 }

I then have a separate class where I create specific instances to represent possible enemies to encounter in the game as follows:

 public static class Enemies
 {
     static Enemy slime = new Creature ("Slime", 12, 2, 0, 2);
     static Enemy wolf = new Creature ("Wolf", 40, 3, 2, 5);
 
     public static List<Enemy > all = new List<Enemy > {
         slime,
         wolf,
     };
 
     public static Enemy Get (List<Enemy > enemyList)
     {
         return enemyList[Random.Range (0, enemyList.Count)];
     }
 }

However if I use this method, the Get method returns that specific instance of the enemy, when I would like for it to return a new instance (so that hp is full etc.).

I have a feeling there is a much better way of doing this, could anyone point me in the correct direction?

Many thanks,

Robin

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by bogs101 · Jan 24, 2018 at 07:41 AM

Hi Niboruga,

you're having the same problem i am currently having.

Did you happen to find a solution?

I stole this code from https://youtu.be/Dke0dlCgFSk and modified it for my own purposes.

Problem is that it uses a static dictionary. I assume this means that i won’t be able to create more than one instance of it. I however would like to create instances of this dictionary. if i remove the static then i get an error "an object reference is required for the non-static field, method or property...."

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

68 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

Related Questions

Runtime Error IOS AOT Only 0 Answers

All Units Go to Same Waypoint 1 Answer

Arrays and Classes Javascript 1 Answer

How can I split up player control logic? 1 Answer

Best way to store and use player classes? 0 Answers


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