Search Unity

Collision Detected but Nothing Happens

Discussion in 'Scripting' started by ThePhiRatio, Aug 23, 2019.

  1. ThePhiRatio

    ThePhiRatio

    Joined:
    Sep 24, 2018
    Posts:
    1
    I hope this is an easy fix and I am missing something easy, but at this point, I am banging my head up against the wall!

    I am building a game where there are targets which you throw fireballs and snowballs at corresponding red and blue targets. On each target (six targets each for 12 in total) have their corresponding script (FireTargetScript and SnowballTargetScript, both are attached below). I have verified with the debug.log that when a snowball hits a snowtarg it does enter the onCollisionEnter function as well as the if statement (same thing when a fireball hits a firetarget) but it does not execute any of the code in that if logic!!!! Very Frustrating! I iterate another variable to keep track of how many targets have been hit using the Value script which is attached to my camera.

    The value script is also attached below with pictures of the inspector for the player, and a firetarg in case that is needed. (I would have added the snowballtarg inspector but it is basically the same thing as the firetarg and it won't let me upload more than 5 files).

    Thank you all for your help! firetarg inspector.PNG firetarg inspector.PNG player inspector.PNG player inspector.PNG
     

    Attached Files:

  2. alexeu

    alexeu

    Joined:
    Jan 24, 2016
    Posts:
    257
    Collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached. (Unity)
     
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Verify that you aren't getting any console errors. For example, if you hit a null reference error in your OnCollisionEnter it will stop executing the rest of the function. I say this because you are just blindly using the results of all those GetComponent calls without first checking if they returned null or not. If they return null, and you use them anyway, you will get a null reference error and never get to the StartCoroutine line.

    Edit: From the images you posted, I'm going to guess you are getting a null reference on the line with GetComponent<Value>(), since your Value script is not attached to the object you're attaching this script to.