Upgrading a game to unity 5

Hi All,

Having trouble understanding what is going wrong here, ive updated the scripts from untiy 4 into unity 5, but i keep getting Null reference exception errors. on line 22 and 29

completeBanner.SetActive (false); & slider.value = Time.timeSinceLevelLoad / levelSeconds;

error only appears in play, and states object reference not set to an instance of an object.

Thanks

Alex

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class GameTimer : MonoBehaviour {

    public Slider slider;
    private float levelSeconds = 100;
    public AudioSource audioSource;
    public LevelManager levelmanager;
    public bool isEndOfLevel = false;
    public GameObject completeBanner;


    // Use this for initialization
    void Start () {
        slider = GetComponent<Slider> ();
        audioSource = GetComponent<AudioSource> ();
        levelmanager = GameObject.FindObjectOfType<LevelManager> ();
        completeBanner = GameObject.Find("CompleteBanner");
        Debug.Log (completeBanner);
        completeBanner.SetActive (false);


    }
   
    // Update is called once per frame
    void Update () {
        slider.value = Time.timeSinceLevelLoad / levelSeconds;

        if (Time.timeSinceLevelLoad >= levelSeconds && !isEndOfLevel) {
            isEndOfLevel = true;
            audioSource.Play ();
            Invoke ("LoadNextLevel", audioSource.clip.length);
            print ("level end");
            completeBanner.SetActive (true);
        }
    }

    void LoadNextLevel() {
        levelmanager.LoadNextLevel ();
    }
}

The line 22 one is pretty easy: GameObject.Find sucks and always has. Here’s some suggestions as to how you might get rid of it.

The line 29 error is odd, only because there’s nothing there that should have changed between 4.x and 5.x. The usual cause would be that there’s no Slider attached to the GameObject this script is on. Can you confirm that that component exists, and post a screenshot of its configuration?

It’s possible that a prefab might have lost its settings during the conversion from 4 to 5 (maybe an exception got thrown somewhere during the upgrade process, which aborted the deserialization process).

You’ll want to doublecheck your prefabs.

1 Like