There is a code that should, in a collision, send the same value to other scripts. But he gives an error. Here is the actual code itself:

void OnCollisionEnter2D (Collision2D collision) { if (collision.gameObject.name == "plat1") { delete1 plat1 = collision.gameObject.GetComponent<delete1>(); plat1.Hit(plusone); delete2 plat2 = collision.gameObject.GetComponent<delete2>(); plat2.Hit(plusone); delete3 plat3 = collision.gameObject.GetComponent<delete3>(); plat3.Hit(plusone); } 

Here is the error code:

NullReferenceException: Delplusone.OnCollisionEnter2D (UnityEngine.Collision2D collision) Object Reference (at Assets / scripts / ScriptsPlus / delplusone.cs: 17)

What can be wrong? Thanks in advance for your help!

  • What line error is it though? - Suvitruf

1 answer 1

The most likely case in this construction is delete1 plat1 = collision.gameObject.GetComponent<delete1>(); . Here you are trying to get the component and in the next line you use it. Just before using do not check that the object is found, so it can swear that the link is empty. The second option is plat1.Hit(plusone); if you have found a component, it’s not a fact that the plusone object plusone not empty at this moment and something does not break when processed somewhere.