[SOLVED] Unity Unable to Detect Object References

In my script, I have a value that I want to equal a value from another script. However, it does not work. For testing purposes, I put the same exact code in another script and it worked fine. There is nothing in the script that doesn’t work that would change the object reference. And in the inspector, the object reference is set. But I still get the error for object reference not being set. Code and images below:

Not working code:

var infobar : GameObject;

function Update ()
{
    print (infobar.GetComponent (Infobar_Control).currentDistance);
}

Working code:

var infobar : GameObject;

function Update ()
{
    print (infobar.GetComponent (Infobar_Control).currentDistance);
}

Yes, they are the exact same, unless someone can see something I haven’t seen the last 10 times I looked. And again, the object reference is set and is not being changed at all. See the image below:

For the full code, look at the spoiler below (omitted functions that don’t have to do with this issue, so error is actually on line 51):

Full Script

#pragma strict

/********************************************************************************************************************
     Purpose: To control the gameplay achievements.
*******************************************************************************************************************/

/* Component & Game Object Variables */
var infobar : GameObject;

var achievementPanel : GameObject;
var achievementPanelImage : UI.Image;
var achievementPanelInfo : UI.Text;

/* Gameplay Variables */
var closeCallsCurrent : int;  // The # of close calls gotten this round
var closeCallsTotal : int;  // The # of close calls gotten total
var coinsCollectedCurrent : int;  // The # of coins collected this round
var coinsCollectedTotal : int;  // The # of coins collected total
var coinsInHand : int;  // The # of coins in hand
var distanceCurrent : float;  // The distance traveled this round
var distanceTotal : float;  // The distance traveled total
var fishInUse : String;  // The name of the fish being used
var gamesPlayedToday : int;  // The # of games played today
var gamesPlayedTotal : int;  // The # of games played total
var playtimeTotal : String;  // The total time spent playing (hours:minutes)

/********************************************************************************************************************
     START OF SCRIPT !   START OF SCRIPT !   START OF SCRIPT !   START OF SCRIPT !   START OF SCRIPT !
*******************************************************************************************************************/

function Awake ()
{
     achievementPanel.SetActive (false);  // Disable the achievement panel

     closeCallsCurrent = PlayerPrefs.GetInt ("Close Calls This Round");
     closeCallsTotal = PlayerPrefs.GetInt ("Total Close Calls");
     coinsCollectedCurrent = PlayerPrefs.GetInt ("Coins This Round");
     coinsCollectedTotal = PlayerPrefs.GetInt ("Coins In Total");
     coinsInHand = PlayerPrefs.GetInt ("Coins In Hand");
     distanceCurrent = 0.0;
     distanceTotal = PlayerPrefs.GetFloat ("Total Distance");
     fishInUse = PlayerPrefs.GetString ("Selected Fish");
     gamesPlayedToday = PlayerPrefs.GetInt ("Games Played Today");
     gamesPlayedTotal = PlayerPrefs.GetInt ("Games Played Total");
     playtimeTotal = PlayerPrefs.GetString ("Total Play Time");
}

function Update ()
{
    //distanceCurrent = infobar.GetComponent (Infobar_Control).currentDistance;
    print (infobar.GetComponent (Infobar_Control).currentDistance);
    distanceTotal = PlayerPrefs.GetFloat ("Total Distance") + distanceCurrent;
 
     if (PlayerPrefs.GetInt ("Has Completed Meters Traveled 25") != 1 && distanceCurrent == 25)  // If has not completed this achievement AND has met the requirement for this achievement...
     {
          CompletedMetersTraveled25 ();  // Trigger the completion of this achievement
     }
}

function CompletedMetersTraveled25 ()
{
     achievementPanel.SetActive (true);  // Enable the achievement panel
     achievementPanelInfo.text = "Noob";  // Set the title of the achievement

     PlayerPrefs.SetInt ("Coins In Hand", PlayerPrefs.GetInt ("Coins In Hand") + 50);  // Award a coin bonus
     PlayerPrefs.SetInt ("Has Completed Meters Traveled 25", 1);  // Set the achievement to complete status
     PlayerPrefs.SetInt ("Achievement Progress", PlayerPrefs.GetInt ("Achievement Progress") + 1);  // Add 1 to the achievement progress
}

/********************************************************************************************************************
     END OF SCRIPT !     END OF SCRIPT !     END OF SCRIPT !     END OF SCRIPT !     END OF SCRIPT !
********************************************************************************************************************/

Using Unity 5.6.0f3

Any help would be greatly appreciated!

- Chris

I would say make sure you don’t have an extra copy of the script in your scene. That’s normally the first thing I would check.

Also make sure your infobar has the Infobar_Control component on it. I believe this gives the type of null exception you are getting. I believe that for stuff exposed in the inspector, the null exception says something about needing to be assigned in the inspector, so slightly different message.

If you have checked both of those things, then from what you posted, I don’t off hand see anything wrong.

1 Like

Yup. That was it. For the reference, I used an object that didn’t actually have the script component attached. Was a real duh moment. :sweat_smile:

Thanks :slight_smile:

- Chris