- Home /
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.
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.
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 currentBulletType
variable) 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.
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.
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 if
going to check for in the currentBulletType
variable? It is not even a boolean or anything like that, just a GameObject. Sorry if this question is very dumb.
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.
Your answer
Follow this Question
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