Search Unity

Object reference not set to an instance of an object

Discussion in 'Scripting' started by Spainforce, Aug 24, 2018.

  1. Spainforce

    Spainforce

    Joined:
    Aug 28, 2016
    Posts:
    3
    I recently downloaded a proyect from the asset store and when I tested it, Unity threw this exception. Well, i didnt notice where the problem is. I have let the part of the code where i think the problem is.
    Someone could help me? Thanks in advance.

    NullReferenceException: Object reference not set to an instance of an object
    Meteor.MagneticEffect () (at Assets/Game/Scripts/Controller/Meteor.cs:159)
    Meteor.Update () (at Assets/Game/Scripts/Controller/Meteor.cs:71)


    Code (CSharp):
    1.  
    2. void Update ()
    3.     {
    4.         ModeSelect();
    5.  
    6.         if (GameController.instance.isGameOver == false && sceneMode == SceneMode.gamePlay)
    7.         {
    8.             MagneticEffect();
    9.         }
    10.     }
    11. void OnTriggerEnter2D(Collider2D other)
    12.     {
    13.         if (other.CompareTag("PlayerAttract"))
    14.         {
    15.             shipTransform = other.transform;
    16.             magnetZone = true;
    17.         }
    18.     }
    19.  
    20.     void OnTriggerExit2D(Collider2D other)
    21.     {
    22.         if (other.CompareTag("PlayerAttract") & looseMagnet)
    23.             magnetZone = false;
    24.     }
    25.  
    26.     void MagneticEffect()
    27.     {
    28.         if (magnetZone)
    29.         {    
    30.             Vector3 directionToShip = shipTransform.position - trans.position;
    31.             rigidBD.AddForce(directionToShip * magnetDirection * Time.deltaTime, ForceMode2D.Force);        
    32.         }
    33.     }
    34.  
    35.  
     
  2. barskey

    barskey

    Joined:
    Nov 19, 2017
    Posts:
    207
    Does your rigidBD actually have a reference to the RigidBody? It would either be set in Start or in the Inspector.
     
  3. Spainforce

    Spainforce

    Joined:
    Aug 28, 2016
    Posts:
    3
    I reference it now from the inspector changing it to public but it doesnt work :( thanks for you reply)
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Your error includes a line number where it is happening, but you didn't bother to mention what line that is of the code you posted. Add debugging before whatever line it is to determine what reference is null. Then figure out why it is null when it shouldn't be and fix.
     
  5. Spainforce

    Spainforce

    Joined:
    Aug 28, 2016
    Posts:
    3
    Mmm, i reasigned the transform component and the error disappear but it doesnt work yet, i will think a diferent way. The lines in the code are 8 and 30. Thanks for reply.
     
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Yeah, so line 30 means either shipTransform or trans are what is null.