Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

Why there is null reference exception in here?

A topic by silvakim created Dec 07, 2021 Views: 331 Replies: 5
Viewing posts 1 to 4

Im coding with spacesurviver multiple kit. but there is null reference exception isue and no line renderer when player has distance  interactable task. 

public class TaskInteraction : MonoBehaviourPun

{

    [SerializeField] private float _range = 10.0f;

    private LineRenderer _lineRenderer;

    private Interactable _target;

    private void Awake()

    {

        if (!photonView.IsMine) { return; }

        _lineRenderer = GetComponent<LineRenderer>();

        StartCoroutine(SearchForInteraction());

    }

    private void Update()

    {

        if (!photonView.IsMine) { return; }

        if (_target != null )  

        {

            _lineRenderer.SetPosition(0, transform.position);

            _lineRenderer.SetPosition(1, _target.transform.position);

            Debug.Log("lineyes");

        }

        else

        {

            _lineRenderer.SetPosition(0, Vector3.zero);

            _lineRenderer.SetPosition(1, Vector3.zero);

            //Debug.Log("lineno");

        }

    }

    private IEnumerator SearchForInteraction()

    {

        while (true)

        {

            Interactable newTarget = null;

            Interactable[] interactionList = FindObjectsOfType<Interactable>();

            Debug.Log("searchtarget");

            UIControl.Instance.HasInteractable = false;    ---------------here's where null reference exception isue

            foreach (Interactable interactable in interactionList)

            {

                float distance = Vector3.Distance(transform.position, interactable.transform.position);

                if (distance > _range) { continue; }

                Debug.Log("searchdistance");

                // An interactible new target found

                newTarget = interactable;

                UIControl.Instance.HasInteractable = true;

                

                break;

            }

            // Reset the previous interactible, if any

            if (UIControl.Instance.CurrentInteractable != newTarget &&

                UIControl.Instance.CurrentInteractable != null)

                Debug.Log("searchnewtarget");

            {

                UIControl.Instance.CurrentInteractable.use(false); 

                

            }

            _target = newTarget;

            UIControl.Instance.CurrentInteractable = _target;

            yield return new WaitForSeconds(0.25f);

        }

    }

    

            

}

From where the error is occurring it seems like UIControl is a singleton that has not been initialized.  If you're getting this in play mode in the editor, make sure the scene you are testing has one.

(1 edit)

thanks for reply. I did like this before.

private void Awake()
{
Instance = this;

}

Moderator moved this topic to General Development

TaskInteraction.Awake() might be getting called before UIControl.Awake().  Try moving the StartCoroutine(SearchForInteraction()); from TaskInteraction.Awake() to TaskInteraction.Start().  Since all Awake() run before all Start(), I recommend doing only initialization-type work in Awake(), so that all the references are set up before you start using them.

private void Start()
{
StartCoroutine(SearchForInteraction());
}

I did like this. but it still exist. did I do rightly?

Deleted 3 years ago

I solve this isue. I used Debug.Log out of brace LOL. thank you for your kind reply!