Search Unity

Object reference not set to an instance of an object

Discussion in 'Scripting' started by vipes, May 28, 2015.

  1. vipes

    vipes

    Joined:
    May 25, 2015
    Posts:
    8
    I came across Alucard J's scripts. They were nice, but some I repurposed for what I needed. However importing his CollectPapers script just gave one problem after another. Knowing very little coding, I just gave up, yet I need said script. The current error is this;
    The code is this;
    Code (JavaScript):
    1. #pragma strict
    2. @script RequireComponent( AudioSource )
    3.  
    4. var papers : int = 0;
    5. var papersToWin : int = 7;
    6. var distanceToPaper : float = 5.5;
    7. var sphereRadius : float = 3.0; // the width of the sphere that is being SphereCast
    8.  
    9.  
    10. public var paperPickup : AudioClip;
    11.  
    12. var theEnemy : enemyscript;
    13.  
    14.  
    15. function Start()
    16. {
    17.      Cursor.visible = true;
    18.      
    19.      // find and store a reference to the enemy script (to reduce distance after each paper is collected)
    20.      if ( theEnemy == null )
    21.      {
    22.          var nme : GameObject = GameObject.Find( "Enemy" );
    23.        
    24.          if ( nme )
    25.          {
    26.              theEnemy = nme.GetComponent( enemyscript );
    27.          }
    28.      }
    29. }
    30.  
    31.  
    32. function Update()
    33. {
    34.      //if ( Input.GetMouseButtonUp(0) ) // use E in editor as LockCursor breaks with left mouseclick
    35.      if ( Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.E) )
    36.      {
    37.          //var ray = Camera.main.ScreenPointToRay( Input.mousePosition ); // always cast ray from center of screen
    38.          var hit : RaycastHit; var rayOrigin : Ray = Camera.main.ScreenPointToRay( Vector3( Screen.width * 0.5, Screen.height * 0.5, 0 ) );
    39.          if ( Physics.SphereCast( rayOrigin, sphereRadius, hit, distanceToPaper ) )
    40.          {
    41.              //if ( hit.collider.gameObject.tag == "Paper" )
    42.              if ( hit.collider.gameObject.name == "Paper" )
    43.              {
    44.                  papers += 1;
    45.                  //Debug.Log( "A pokeball was picked up. Total objects = " + objects );
    46.                  
    47.                  GetComponent.<AudioSource>().PlayClipAtPoint( paperPickup, transform.position );
    48.                  
    49.                  Destroy( hit.collider.gameObject );
    50.                  
    51.                  // make enemy follow closer
    52.                  //theEnemy.ReduceDistance();
    53.                
    54.                  // make enemy follow closer
    55.                  if ( papers == 1 )
    56.                  {
    57.                      theEnemy.SetFirstPaperDistance();
    58.                  }
    59.                  else
    60.                  {
    61.                      theEnemy.ReduceDistance();
    62.                  }
    63.              }
    64.          }
    65.      }
    66. }
    Please, help. It's pretty much the last thing I need to wrap it up, code wise.
     
  2. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    theEnemy is null when you call ReduceDistance(); on line 61. add something like if(theEnemy!=null) befrore you call that line
     
  3. vipes

    vipes

    Joined:
    May 25, 2015
    Posts:
    8
    I'll try that. What's odd is that it worked fine on Unity 4.

    No errors. That's good. But the item doesn't want to be picked up. Made sure both the name and tag is "Paper", made sure is rigidbody, and made sure it uses a box collider.
     
    Last edited: May 28, 2015
  4. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    969
    Have you set the theEnemy value in the inspector? If not, this script will try to find a gameobject named Enemy. If it's not set in the inspector, or you have no gameobject named Enemy, you will get a nullreference exception.
     
  5. vipes

    vipes

    Joined:
    May 25, 2015
    Posts:
    8
    Yes I have.


    Even made the sphere radius bigger then said item just to be sure.
     
  6. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    969
    That's strange, it's definitely saying the enemy is null. You're not destroying the enemy or anything like that from a different script?