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 $$anonymous$$ · Jul 13, 2017 at 03:52 PM · c#2dscript.enemiesspace invaders

How should I properly store statistics of various items of same type?

[C#]

Im a begineer making a game based on Space Invaders, with some modifications.

In my game every enemy is going to shoot a bullet with different damage values and different sprites. My problem is that I cant figure out a way on storing that information and every enemy then get it from the script, and shoot a bullet with that data.

I can make a script for every bullet, but that would be a mess and there must be a better way.

I also thought of making a manager for those bullets with something like a group of bullet types and its sprites, then when the function of shooting is called choosing those, but I dont know how to make that.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Jwizard93 · Jul 13, 2017 at 04:20 PM

So each type of bullet could be a prefab. Each having the one bullet script. That script could have public variables such as "damage" that way each prefab can be edited in the inspector for that prefab to carry different values for the same set of variables. The sprites and such would also be included with the prefabs.

To make a prefab: create what you want in the scene. Drag the gameObject from the scene into the project folder. Delete the object from the scene.

To instantiate a bullet prefab from, say, a script called "Player" it would looks like this:

 public class Player : MonoBeaviour
 {
     public GameObject bulletPrefabType1;
     public GameObject bulletPrefabType2;
     .
     .
     .
     public GameObject bulletPrefabTypeN;
 
     private GameObject currentBulletType;
 
     void Update()
     {
         if (Input.GetKeyDown(Keycode.Space))
         {
             FireBullet(currentBulletType);
         }
     }

     void setCurrentBulletType()
     {
          currentBulletType = //some code to chose which bullet//;
     }
 }

You would then drag the prefabs from the project folder into the proper public field created for each bullet type on the player's inspector.

This is not a complete solution, just something to show you how one MIGHT handle this situation in Unity.

Best of luck to you, feel free to comment with further questions if they pertain to this exact problem.

If for whatever reason you decide each bullet type should actually be a seperate class then you may want them all to inherit from a BaseBullet class or have them all implement an interface IBulletType. This makes it simpler for other scripts to manipulate bullets without having to know ecaxtly which bullet it is for functions that should apply to all bullets.

Comment
Add comment · Show 3 · 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
avatar image $$anonymous$$ · Jul 13, 2017 at 04:32 PM 0
Share

Hello and first of all, thanks for the answer.

That is exactly what I wanted. But I still have a question: Is there any way I could store the bullet types and choose one (in your code, it would be stored in the currentBulletTypevariable) when the function to shoot is called? For example: Some enemy or player calls shoot(BulletType.Huge). How would I make the "BulletType.something or something close to that?

I can guess being an array, but since Im a beginner, I have no idea.

avatar image Jwizard93 $$anonymous$$ · Jul 13, 2017 at 04:59 PM 0
Share

Hmm. Someone else could probably produce this in less code than I, but I would have an Enum and a switch statement:

 public Enum BulletType {HUGE, REGULAR, S$$anonymous$$ALL, CRAZYFAST, NU$$anonymous$$E};
 
 public void Shoot(BulletType _type)
 {
 
     curretnBulletType = null;
 
     switch(_type)
     {
         case BulletType.HUGE:
             currentBulletType = hugeBullet;
             break;
 
         case BulletType.REGULAR:
             currentBulletType = regBullet;
             break;
 
         .
         .
         .
 
         default:
             Debug.Log("something went wrong with bullet type");
             break;
     }
 
    if (currentBulletType)
    {
         shootBullet(currentBulletType);
     }
 }

$$anonymous$$aybe start with something like this and find ways to clean it up, make it simpler.

avatar image $$anonymous$$ Jwizard93 · Jul 14, 2017 at 08:56 PM 0
Share

Sorry for not responding earlier but I was busy. That code seems fine for me ( right now Im learning, I just want stuff to work, not to be as optimized as possible) but I cant understand this statement:

if (currentBulletType)

 {

      shootBullet(currentBulletType);

  }

What is the ifgoing to check for in the currentBulletTypevariable? It is not even a boolean or anything like that, just a GameObject. Sorry if this question is very dumb.

avatar image
0

Answer by Pigenator · Jul 19, 2017 at 04:12 PM

I would use a public array to store bullet types:

 public GameObject bullets[];
 
 void Shoot(int type){
    //Shoot bullet 
    Instantiate(bullets[type], Transform.position,
    Transform.rotation)
 }

Add the prefabs to the public array and you should be fine :)

Also, mark correct answers as such to mark the question as answered.

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

375 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 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 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 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

How can I call Animations per C# Script? 1 Answer

Multiple Cars not working 1 Answer

Multiple gameobjects but only one is screen wrapping. 0 Answers

string/text database 0 Answers

Distribute terrain in zones 3 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