• 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 gaia2222 · Sep 26, 2018 at 10:22 AM · delayaddcomponent

Anyway to add method to other gameobject?

For example idea bullet:if collision to enemy(add a explosion method to enemy while enemy is died) like the button function.

enemy: i set a state(idle , walking , attack , died) I want to save some List and release them while it is died

And the other question:i know that it can set the destroy time while it instantiate. Is there a simple way to change the other script variable for a few second??

thankyou for your answer^W^

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
0

Answer by Baalhug · Sep 26, 2018 at 11:54 AM

If I understand your question correctly, what you want is not to "add code" in runtime, what you want is to execute that code in a different gameobject. That can be done this way:

Let's say your enemy's script is called "enemyscript". In enemyscript you add a function, for example:

 void EnemyDies()
 {
    // here you add the explosion effects, animations, states...
 }

Now you call this function from the bullet's script like this:

 void OnCollisionEnter(Collision collision)
 {
    collision.gameObject.getComponent<enemyscript>().EnemyDies();
 }

The same answer for the last question, to access another object script, you must use:

 object.getComponent<script>()

where object is a reference to that object and script is the name of that object's script

Comment
Add comment · Show 4 · 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 gaia2222 · Sep 26, 2018 at 02:16 PM 0
Share

thankyou for your reply^^I meam.... Is it possible to write the original script in the enemyscript .and let the bullet add the script to the enemy??

OR add the new script to the enemy gameobject . And the script check for the enemy state.When the enemy's state is deid and do something.... If this method is work ... how do it do that??

avatar image Justinger gaia2222 · Sep 26, 2018 at 03:20 PM 0
Share

If you wanted to have one object to add a script to another, you could have it actually add whatever script you like upon collision. You could even double check to make sure it only has one at a time.

     void OnCollisionEnter(Collision collision)
      {
         if(collision.gameObject.addComponent<myBulletHitScript>() == null) collision.gameObject.addComponent<myBulletHitScript>();
      }

Otherwise, what Baalhug said about getting the component sounds very useful. To expand on it a little, I might pass some information on with it.

 // Your bulletScript would contain this method
 void OnCollisionEnter(Collision collision)
 {
         if(collision.gameObject.getComponent<enemyscript>() != null) collision.gameObject.getComponent<enemyscript>().EnemyDamage(this);
 }
         
 // You can pass anything you want to your enemy script. In this case, we pass a reference to this actual bullet, so that you can literally access anything that is public about it, like the bullets damage, etc.
         
 // Your enemyScript would contain this method, that runs whenever a bullet hits it.
 public void EnemyDamage(bulletScript  _bs)
 {
 health -= _bs.damageAmount;
 if(health <= 0) Death();
 impact = _bs.impactStrength;
 }
avatar image gaia2222 · Sep 26, 2018 at 04:54 PM 0
Share

thankyou for your reply^^ $$anonymous$$aybe i may i understand your method working.Cause of my original idea , i don't want to write all the code to the enemyscript and make it very burden .So i think that make a List(script) add from the bullet and store in the enemy script and release while it died.But after i hear your method , i know that it maybe not possible in unity.

Actually , $$anonymous$$y idea is the bullet attack the enemy and spawn the Guardian to help player while the enemy is died(this function is not including in the enemy original code)

So i want to ask the last question^^:If i really addComponent to the enemy script.How can i check doing something while the enemy died? Is it a different way for don't check it in the Update()? thank you for your patient and help me solve the problem

avatar image Justinger gaia2222 · Sep 26, 2018 at 06:05 PM 0
Share

" How can i check doing something while the enemy died?"

I don't know how you have written your enemy, but my personal approach would be to simply have the bullets cause damage to the enemy. When the enemy detects it's health below 0, it would call a method that that does whatever you like when it dies for any reason.

 private health = 100;
 
 public Health
 {
      get
      {
          return health;
      }
      set
      {
          health = value;
          if(health <= 0) Death();
      }
 }

 // Death method for your enemy
 public void Death()
 {
      // Code to spawn your guardian. Happens whenever this enemy dies.
 }

Then, you can have your bullet deal damage by using your enemies public property, enemyScript.Health -= 25, for example.

I understand if you don't want to put it into your enemy script, but it might be the best route. An enemy script typically is what manages what happens with it's own death.

avatar image
0

Answer by DownER149 · Sep 26, 2018 at 05:15 PM

(this function is not including in the enemy original code) add the function and only call it if gameobject.name (or if you have more than one use a bool that is flipped on when that type of object needs that type of function) is equal to the thing that you want that function to run on "if its doing something" is rather vague. store the component on a variable.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

93 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

Related Questions

Scroll Rect draging delay 0 Answers

Make enemy wait before attacking player 2 Answers

AudioSource playing with delay for 1 step 0 Answers

How do I put a delay in this? 2 Answers

Touch input reporting 0 delta for first several Updates on iOS, 2 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