• 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 mmaalex22112 · Sep 26, 2017 at 12:08 AM · c#scripting problemcollider2d gamescripting beginner

Collider not registering hits?

Heyo, I've been trying to create a prefabed Projectile that is instantiated and shot at a player to deal damage. The problem is the shots don't seem to be registering when they hit the player.

The code for the projectile:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Projectile1 : MonoBehaviour
 {
     public GameObject player;
 
 
     public void Start()
     {
        player = GameObject.FindGameObjectWithTag("Player");
     }
     void OnCollisionEnter(Collision col)
     {
             if ( col.gameObject.tag == "Player")
             {
             player.GetComponent<HopeHealth>().HopeDamageAmount = 20;
             player.GetComponent<HopeHealth>().TakeDamage();
             Debug.Log("player has been hit");
         }
     }
  }

Im closer to a beginner so I'd appreciate an answer not too complicated Lol. Thanks in advance for the help. :)

Thank you to @OutOfRam and @Ifran-unity for your quick responses!

Comment
Add comment · Show 1
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 tmalhassan · Sep 26, 2017 at 04:28 AM 0
Share

Did you make sure that the tag is actually the same? (keep in mind that it's case sensitive). Also, did you make cure that the collider is not set as isTrigger?

4 Replies

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

Answer by OutOfRam · Sep 26, 2017 at 04:29 AM

Hey there,

The issue here is that you are attempting to grab the gameobject component from a collision event. when you call "void OnCollisionEnter(Collision col)" the parameter you are passing in is "Collision", not "Collider". the collision parameter holds info like the point of contact the force of the hit, the collider hit, and some other things. what you need to do is change the subsequent if statement...

 if ( col.gameObject.tag == "Player")

to

 if ( col.collider.gameObject.tag == "Player")

Hope this helps!

Comment
Add comment · Show 2 · 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 mmaalex22112 · Sep 26, 2017 at 04:38 AM 0
Share

@OutOfRam Thank you too for your reply. I tried this:

     void OnCollision(Collision other)
     {
         Debug.Log("hit");
         if (other.collider.gameObject.tag == "Player")
             {
             player.GetComponent<HopeHealth>().HopeDamageAmount = 20;
             player.GetComponent<HopeHealth>().TakeDamage();
             Debug.Log("player has been hit");
         }
     }
 





To no avail :/ In fact it's not even registering my debug.log.

avatar image OutOfRam mmaalex22112 · Sep 26, 2017 at 05:20 AM 0
Share

Hello,

I did not at first glance realise your game was 2D. You are currently using the Unity 3D collision method try this

 void OnCollisionEnter2D(Collision2D col) 
 {
      Debug.Log("hit");
      if (col.collider.gameObject.tag == "Player")
          {
          player.GetComponent<HopeHealth>().HopeDamageAmount = 20;
          player.GetComponent<HopeHealth>().TakeDamage();
          Debug.Log("player has been hit");
      }
  }


@mmaalex22112

avatar image
1

Answer by irfan-ayub · Sep 26, 2017 at 04:45 AM

There are multiple possibilities.

===>> You must have a RigidBody component on any one of two bodies you are trying to collide. check in the inspector that you have a RigidBody component added to any one of these bodies. alt text

or it would be 2D if you are using 2D bodies.

===>> You might have Enabled onTrigger flag in the collider component on any of your bodies, for detecting collisions you must have it off. (This flag is to trigger only and the bodies will cross each other without any collision.)

alt text

===>> Your Projectile might be moving very fast and the collider component is not able to detect the collision on that particular frame or time. try making your collider on player bigger or slowing down the speed of your projectile.

Hope It Helps.. Happy Coding and Development.. :) and Don't Lose your hope..


1.png (12.0 kB)
2.png (10.7 kB)
Comment
Add comment · Show 3 · 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 mmaalex22112 · Sep 26, 2017 at 04:57 AM 0
Share

@irfan-unity, Thank you as well, I checked on all three solutions and still nothing.

  1. Both the projectile and the player have rigidbody2Ds

  2. Neither has the Is Trigger flag enabled

  3. Halved the speed to see the same results

avatar image irfan-ayub mmaalex22112 · Sep 26, 2017 at 05:12 AM 0
Share

you are working in 2D and the function you are using is OnCollisionEnter().

for 2D you must use OnCollisionEnter2D(Collision2D other). See this for details.

avatar image irfan-ayub irfan-ayub · Sep 26, 2017 at 05:12 AM 0
Share

@mmaalex22112

avatar image
0

Answer by Vilhien · Sep 26, 2017 at 12:59 AM

I'm semi new myself (two weeks). My best guess is you have no reference to the script that is trying to tag HopeHealth right off the bat.. Try this.

Below your public callout to the GameObject player;

HopeHealth hope;

That will reference your script. Then right below where you call out he player in start. try this..

hope = player.GetComponent(HopeHealth);

@mmaalex22112

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 mmaalex22112 · Sep 26, 2017 at 02:03 AM 0
Share

Hey @Vilhien, thanks for your response, see the issue doesn't lie with the actions called on collision, because the collision doesn't seem to even register. Hence why I added the debug.log to the beginning of the event.

avatar image
0

Answer by Agent27765 · Sep 26, 2017 at 02:39 AM

Hello I find myself as a beginner although I have been programming for 3 months or so. In this case this is possibly a different way I would do it. Its possible you forgot to tag the object with the EXACT same tag as "Player" If that is not the case. Try something like this using System.Collections; using System.Collections.Generic; using UnityEngine;

  public class Projectile1 : MonoBehaviour
  {
      public GameObject player;
  
  
      public void Start()
      {
         player = GameObject.FindGameObjectWithTag("Player");
      }
      void OnCollisionEnter(Collision other)
      {
              if ( other.gameObject.tag == "Player")
              {
              player.GetComponent<HopeHealth>().HopeDamageAmount = 20;
              player.GetComponent<HopeHealth>().TakeDamage();
              Debug.Log("player has been hit");
          }
      }
   }

See the change with other in the void OnCollisionEnter(Collision other) and also other.gameobject.tag. I believe that I have had the same problem with that a while back and I believe that is the way I have fixed it. I hope you found this helpful!

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 mmaalex22112 · Sep 26, 2017 at 04:20 AM 0
Share

Hey @Agent27765, thank you also for the response. I tried both solutions and still nothing! I'm losing hope!

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

428 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 do collision in 2d game? 1 Answer

How can I add the OnTriggerEnter function to all game objects that I instantiate? 1 Answer

(C#) SetActive isn't working on my UI 2 Answers

What happened to Line Renderer! 1 Answer

How would I make a ListChangedEvent? 1 Answer

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