Search Unity

Object reference not set to an instance of an object

Discussion in 'Scripting' started by caseyboundless, Jul 26, 2014.

  1. caseyboundless

    caseyboundless

    Joined:
    Oct 17, 2011
    Posts:
    590
    I'm trying to access the "CheckTime.cs" script to see if it is day time or night time. But I keep getting this error.
    What I'm doing wrong with get component?


    NullReferenceException: Object reference not set to an instance of an object
    UiExample.Start () (at Assets/WeatherReport/Examples/UiExample.cs:88) line 7


    NullReferenceException: Object reference not set to an instance of an object
    UiExample.Update () (at Assets/WeatherReport/Examples/UiExample.cs:124) line 43






    Code (CSharp):
    1. void Start()
    2.     {
    3.  
    4.  
    5.  
    6.         GameObject goFindCamera= GameObject.Find ("MainCamera");
    7.         isDayTimes = goFindCamera.GetComponent<CheckTime> ();
    8.         if (goFindCamera)
    9.         {
    10.          Debug.Log (" found main camera with check time script");
    11.  
    12.         }
    13.         else if (! goFindCamera)
    14.         {
    15.             Debug.Log (" cant find main camera with check time script");
    16.         }
    17.  
    18.  
    19.  
    20.  
    21.     }
    22.  
    23.     void Update()
    24.     {
    25.  
    26.         humiditylabel.Text = Weather.Humidity.ToString ();
    27.         windspeedLabel.Text = Weather.WindSpeedMph.ToString () + "  mph";
    28.         weatherConditionLabel.Text = Weather.Condition.ToString ();
    29.         myLocationLabel.Text = Weather.Location.ToString();
    30.         mytemparature.Text = Weather.TemperatureF.ToString () + "  Farenheit";
    31.         myLogitude.Text = Weather.Longitude.ToString ();
    32.         myLatitdue.Text = Weather.Latitude.ToString ();
    33.  
    34.  
    35.     //    if (myWeatherPartlyCloudy == WeatherCondition.PartlyCloudy && isDayTimes.isDayTime == true)
    36.     //    {
    37.     //      PartlyCloudyDay.SetActive (true);
    38.      
    39.     //    }
    40.     //
    41.  
    42.  
    43.         if (myWeatherPartlyCloudy == WeatherCondition.PartlyCloudy && isDayTimes.isDayTime == false)
    44.         {
    45.                         PartlyCloudyNight.SetActive (true);
    46.         }
    47.         else {
    48.  
    49.             PartlyCloudyNight.SetActive (false );
    50.             }
    51.  
    52.  
     
  2. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    Your variable isDayTime, is that of type CheckTime?

    Paste your variable so I can see how you're creating your handle.

    Anytime you see that error it's due to the fact that the component isn't being reached.

    You're assigning a variable (unknown type) to a type CheckTime.

    Otherwise,

    Try:

    private CheckTime isDayTimes;

    start()
    {
    isDayTimes = GameObject.Find("MainCamera").GetComponent<CheckTime>();
    }

    Also, check the spelling of the gameObject you're trying to find.

    You would change your null check to:

    if (isDayTime != null)
    debug.log
     
  3. caseyboundless

    caseyboundless

    Joined:
    Oct 17, 2011
    Posts:
    590
    CheckTime is a script I'm trying to access through posted code. isDayTime is a public bool in CheckTime.cs. The CheckTime script is on the main camera. isDayTimes is the variable part for posted code as public CheckTime isDayTimes.Too late to test will try later.
     
  4. caseyboundless

    caseyboundless

    Joined:
    Oct 17, 2011
    Posts:
    590
    "MainCamera" was supposed to be "Main Camera". lol
     
  5. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008