• 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 Bezari0us · Jun 12, 2018 at 10:49 AM · scripting problemgameobjectshootingraycasthit2d

Duplicate object and shooting problem (SOLVED)

I'm learning to make a 2D platformer. I have a gun rotating around the mouse pointer and shooting at the direction of the mouse pointer when the left mouse button is clicked. The flying enemy has a Box Collider 2D on it. When i use the gun to kill the original flying enemy, it worked pretty well, the flying enemy died. But after i created a duplicate of my original flying enemy, something weird happened. If i shoot at the duplicate one, it would die like normal. But if i shoot the original one first, the duplicate one will be taken damage and will die first then if i keep shooting at it, the original one will be taken damage and die like normal. Can someone point out my problem here? Thank you for taking your time to read my question. Here is the script i attached to the gun.

 void FireRocket()
     {
         Vector2 mouPos = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
 
         Vector2 gunTipPosition = new Vector2(gunTip.position.x, gunTip.position.y);
 
         RaycastHit2D hit = Physics2D.Raycast(gunTipPosition, mouPos - gunTipPosition);
 if (hit.collider != null)
         { 
             if(hit.collider.tag=="Flying Enemies")
             {
                   FindObjectOfType<MakeDead>().RemoveHealth(damage);
             }
         }
 }

And here is the scrip i attached to the flying enemy

 void Start () {
         currentHp = maxHp;
 }
 
 public void RemoveHealth(float amount)
     {
         currentHp -= amount;
         healthBar.fillAmount = currentHp / maxHp;
         if (currentHp <= 0 && !hasDied)
         {
             hasDied = true;
             Destroy(gameObject);
             Instantiate(bloodParticle, transform.position, transform.rotation);
         }
     }




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

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by bakir-omarov · Jun 12, 2018 at 11:05 AM

See, FindObjectOfType() will look for all GameObjects on scene, and first finded GameObject with MakeDead script will your find result.
It is wrong, because you need to kill/damage your target enemy. You can find your target enemy game object from your hit, by just using hit.collider.gameObject . And then just find in this game object your MakeDead script using GetComponent(), and call function RemoveHealth(damage);.

P.S i also cleared your code, please check if its works, because i don't tested it.

Just change your code to it:


     void FireRocket()
     {
         Vector2 mousPos= Camera.main.ScreenToWorldPoint(Input.mousePosition);
 
         RaycastHit2D hit = Physics2D.Raycast(gunTip.position, mouPos - gunTip.position);
         if (hit.collider != null)
         {
             if (hit.collider.tag == "Flying Enemies")
             {
                 hit.collider.gameObject.GetComponent<MakeDead>().RemoveHealth(damage);
             }
         }
     }











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 Bezari0us · Jun 12, 2018 at 11:11 AM 0
Share

Wow, worked like charm. Thank you very much

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

181 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

Related Questions

i am making an increment of one when user clicks a game object, trying to decrement the counter if the same game object is clicked again 1 Answer

Issues cycling through Game objects in dictionary and activating currently selected objects script 1 Answer

How do i change a sprite when another gameobject with the same prefab is colliding / is near 1 Answer

Ingame music goes on but it should stop when you die (DeathCanvas Activate) or win (LevelComplete Canvas Activates) 1 Answer

Change Target from Script in GameObject with another Script 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges