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);
}
}
}