- Home /
The question is answered, right answer was accepted
Change value of a projectile that is instantiated in Awake and pooled
I'm using a projectile to cause damage, like a bullet, and the way I deliver this damage is to attach a damage component to the projectile itself and set the value from the inspector and store this projectile as a prefab; I then have an object pooling script attached to my weapon that instantiates 20 projectiles in Awake to optimize the spawning and destroying of these projectiles, the values for each of these projectiles is stored in the prefab.
I'm running into trouble now with things like power-ups or ways to change the damage value of this projectile during run time, right now it will only happen when Awake is called (which is when I start and run the game or change scenes), and am not finding anything that quickly or easily explains how to change the values of the projectiles after they are instantiated and pooled.
I have tried referencing the prefab and changing the values from there, but of course because the objects are pooled and only check for values on the Awake, runtime changes are not occuring and as mentioned I only see changes when I change scenes or quit runtime and replay.
Will I have to find another way to create and destroy the projectiles, or can what I'm asking for be done with an object pooling system? If it can be done, is there an example someone can share of how to change values during runtime?
Thanks
Answer by SuperScience · Oct 31, 2018 at 04:48 AM
Hi,
You don't need to cache projectiles like this. Its actually better if you destroy them and only have those that are instantiated be present in the game. Create, use, destroy. There is a performance hit only on the first instance created, and you can eliminate that performance hit by having a loading scene that does a Resources.Load on each object you want to instantiate.
Put your prefabs in Assets/Resources/Projectiles
Lets say you have a missile. Lets name is Missile, and put a "Missile.cs" script on it, then prefab it to Projectiles.
Missile missile = (Instantiate(Resources.Load("Projectiles/Missile") as GameObject).getComponent<Missile>();
// the "as GameObject" might be redundant here
missile.setDamageAmount(10);
setDamageAmount will get called around the same time that Awake is called, and you can set damage at that point.
then do damage on collision
void OnCollision(Collider collider){
Enemy enemy = collider.getComponent<Enemy>(); // or whatever you have that takes damage. Its probably best to have a "takes damage" interface.
enemy.damage(amountThatYouSetDuringSetDamage);
Destroy(gameObject);
}
So all of this is the kinda brutal solution to this problem. If you want to get really fancy and elegant with it, use the Particle System for your projectiles. Particles can take models OR sprites, and have a bunch of really convenient built-in stuff that makes them perfect for bullets, muzzle flashes, shell casings, etc. They can take physics and also do collisions (though they have their own collision method. I think its OnParticleCollision(Collider other)
But hopefully this will get you unblocked.
Note that some of this is pseudocode. If you can't figure it out, let me know and I'll give you exact code. I just don't have a Unity with me presently to verify the syntax. (sorry).
I think that the big takeaway though is - you don't need to cache projectiles. Just create them and destroy them as-needed. The performance hit is negligible.
Thanks for the feedback, but I found a way to do this through the poolable ammo like I was hoping for.
In case anyone else out there has this issue, what I found to be a solution is fairly simple:
If your projectile is using a generic damage script that could be used by anything that would inflict damage, create one that would be custom for the Player's projectiles (inherit from your normal damage script), and add in the Start a reference to a damage multiplier script that would contain all the buff values.
In this damage multiplier script, make sure you have a base value for damage for when you don't have a buff activated, then create functions for each instance in which you would assign a buff and what that value would be, then make sure the appropriate value (either base damage or buff damage) is assigned as the final damage.
Finally, override the damage value from the custom Player projectile script you made earlier and use the final damage value that is assigned from you multiplier script you referenced earlier in Start.
This has allowed me to not only have all the buffs and base damages interact with each other when necessary, but also allowed me to assign the damage value directly from the weapon prefab itself now and not from the projectile's damage script like it used to.
I'm sure what I did is very elementary and nothing special, but I had a hard time finding any information on how to change the value of poolable ammo at runtime, and want to share my technique with anyone else who is having that same problem.
Follow this Question
Related Questions
How to Instantiate a game object at the same position where the previous is destroyed 2 Answers
Instantiate Prefab Problem 1 Answer
Instantiate object 1 Answer
The object of type 'Transform' has been destroyed not being solved even when I check if it's null 0 Answers
How to instantiate a Prefab ? 2 Answers