Do not understand this "UnassignedReferenceException"

I get the following message but do not understand why i get it.

myTransform is assigned:

3167761--241219--Skärmavbild 2017-08-01 kl. 16.34.55.png

Here is the code, which is attached to a button that i list multiple times in a ScrollView.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;

public class FriendsListItem : MonoBehaviour {

    private GameObject titleText;
    [SerializeField] private Text txt_BtnTitleName;
    [SerializeField] private Transform myTransform;
    private string[] friendsArray;
    private InputField inp_FinalIPaddress;
    private string myIPAddress;

    void Start () {
        titleText = GameObject.Find ("host_txt_Title");
        titleText.GetComponentInChildren<Text> ().text = "Peter";
        myTransform.GetComponent<Button>().onClick.AddListener(delegate { Btn_FriendsListButton(); });
        inp_FinalIPaddress = GameObject.Find ("inp_IP_Address").GetComponent<InputField> ();
        //print (Application.persistentDataPath + "/UNET_FriendsList.dtt");
    }

    public void Btn_FriendsListButton () {


        titleText.GetComponentInChildren<Text> ().text = txt_BtnTitleName.text;

    }

    void FindPlayersIPaddress () {
        // Open file and read everything in the file into array
        System.IO.StreamReader readFile = new System.IO.StreamReader (Application.persistentDataPath + "/UNET_FriendsList.dtt");
        friendsArray = System.IO.File.ReadAllLines (Application.persistentDataPath + "/UNET_FriendsList.dtt");
        readFile.Close ();

        bool didNotFind = true;

        // Find the IP address
        for (int i = 2; i < friendsArray.Length; i++) {

            if (txt_BtnTitleName.text == friendsArray [i]) {
                myIPAddress = friendsArray [i + 1];
                inp_FinalIPaddress.text = myIPAddress;
                didNotFind = false;
                break;
            }

        }

        if (didNotFind) {
            inp_FinalIPaddress.text = "No IP-address found";
        }

    }

}

First question: is “myTransform” just the transform of the object where the script is? If so, why not just use GetComponent<> directly? The transform reference is redundant.

Second question: You seem to have a lot of these objects; if even one of them is unassigned you’ll get that error. Try inserting a Debug.Log line with gameObject as the second parameter just before line 19 to see which object is throwing the error.

Assuming all your clones are assigned, make sure you don’t have an extra copy of the script somewhere in the scene.

Also, I’m a little confused on your start method.

First, friendly advice, I would not use GameObject.Find, it’s slow and you’re doing it for several objects. Better to have a manager type script that you can access the “host_txt_Title” and “inp_IP_Address” through.

Also, you now have several objects assigning the same text to the same object. Seems a bit odd to do that. (maybe you are just doing this for testing…)

@StarManta already posted about how to find the offending gameobject easier.

1 Like

Or better yet, just serialize the button in the inspector instead of the transform. Or better yet, just add the click event in the inspector in the first place…

1 Like

Thanks, forgot writing that this is pure testing. That is the reason why code looks like it does.

Well, i did find the problem, thank you for your comments. I had the code attached to the buttons but also to my Script_Container, which i did initially.

However, i did adjusted the code as well, before i will redesigning it completely and adjust to the new design i am working on:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;

public class FriendsListItem : MonoBehaviour {

    [SerializeField] private Text txt_BtnTitleName;
    private string[] friendsArray;
    private string myIPAddress;
    private bool firstRun;
    private Color32 yellowColor = new Color32 (200, 200, 2, 255);

    public JoinDirectMatch _joinDirectMatch;

    void Start () {

        _joinDirectMatch = JoinDirectMatch.FindObjectOfType<JoinDirectMatch> ();
        _joinDirectMatch.go_btn_DeleteFriend.SetActive(false);

        firstRun = true;

    }

    public void Btn_FriendsListButton () {

        if (firstRun) {
            firstRun = false;
            _joinDirectMatch.txt_IPName.text = "";
            _joinDirectMatch.inp_IP_Address.text = "";
            gameObject.GetComponent<Image> ().color = yellowColor;
            _joinDirectMatch.go_btn_DeleteFriend.SetActive(true);

        } else {
            firstRun = true;
            _joinDirectMatch.txt_Title.text = txt_BtnTitleName.text;
            FindPlayersIPaddress ();
            gameObject.GetComponent<Image> ().color = Color.white;
            _joinDirectMatch.btn_JoinMatch.image.color = yellowColor;
            _joinDirectMatch.go_btn_DeleteFriend.SetActive(false);
        }

    }

    void FindPlayersIPaddress () {
        // Open file and read everything in the file into array
        System.IO.StreamReader readFile = new System.IO.StreamReader (Application.persistentDataPath + "/UNET_FriendsList.dtt");
        friendsArray = System.IO.File.ReadAllLines (Application.persistentDataPath + "/UNET_FriendsList.dtt");
        readFile.Close ();

        bool didNotFind = true;

        // Find the IP address
        for (int i = 2; i < friendsArray.Length; i++) {

            if (txt_BtnTitleName.text == friendsArray [i]) {
                _joinDirectMatch.txt_IPName.text = friendsArray [i];
                myIPAddress = friendsArray [i + 1];
                _joinDirectMatch.inp_IP_Address.text = myIPAddress;
                didNotFind = false;
                break;
            }

        }

        if (didNotFind) {
            _joinDirectMatch.inp_IP_Address.text = "No IP-address found";
        }

    }

}