Reference to several objects



  • There's a code that has to send the same meaning to other violators when faced. But he's making a mistake. That's the code.

    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's the error code:

    NullReferenceException: Object reference not set to an instance of an
    object delplusone.OnCollisionEnter2D (UnityEngine.Collision2D
    collision) (at Assets/scripts/ScriptsPlus/delplusone.cs:17)

    What's the matter? Thank you for your help.



  • The most likely thing about this design is that delete1 plat1 = collision.gameObject.GetComponent<delete1>();♪ You're here trying to get a component and you use it in the next line. Just before you use it, you don't check that the object is found, so you might argue that an empty link. Second option plat1.Hit(plusone); If you have a component, it's not the fact that the object itself is. plusone You're not empty at this point, and there's something going on in the process.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2