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 /
This question was closed Oct 20, 2020 at 04:32 PM by I_Am_Err00r for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by I_Am_Err00r · Oct 30, 2018 at 10:06 PM · instantiateruntimedestroy objectobject poolchange variable

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

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

  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · 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 I_Am_Err00r · Nov 02, 2018 at 08:01 PM 0
Share

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:

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

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

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

Answers Answers and Comments

115 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

Related Questions

How to Instantiate a game object at the same position where the previous is destroyed 2 Answers

Referencing a variable in another script possibly not working? 0 Answers

Endless Prefab Instantiating with different position and rotation 1 Answer

How to create detonation time for grenade projectile. 0 Answers

How do I destroy the object that was instatiated 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