Checking for Player to Object distance for other scripts to check state.

Im new to unity and scripting and managed to make a script for clicking on an object to mute/unmute it, but only at a close range. The problem I have now is Im trying to seperate them so that one script checks the distance, another mutes/unmutes, and another to change the GUI crosshair colour. I should tell you in advance I only started to learn scripting of any kind yesterday.

This is the script to check distance:
using UnityEngine;
using System.Collections;

public class muteaudioclick : MonoBehaviour
{
	public int triggdist = 3;
	
	public Transform player;
	
	private int distance;
	
	public bool playerclose;
	
	void Onupdate(){
		
		float distance = Vector3.Distance(player.position, transform.position);
		if(distance < triggdist)
		{
		playerclose = true;
		}
		else
		{
		playerclose = false;
		}
	}
}

This is for muting

using UnityEngine;
using System.Collections;

public class muteaudioclick : MonoBehaviour
{
	
	private checkplayerdist playerclose;
	
	void Awake()
	{
		playerclose = GetComponent<checkplayerdist>();
	}
			
	
	void OnMouseDown()
	{
		if(playerclose == true)
		{
	
		
		if(audio.mute == false)
		
			{
				audio.mute = true;
				Debug.Log ("Sound Off");
			}
		else
			{
				audio.mute = false;
				Debug.Log ("Sound On");
			}
		}
	}
}

And finally the crosshair, I havent even tried to incorperate this yet, trying to get the mute function to work.

using UnityEngine;
using System.Collections;

public class CrosshairGUI : MonoBehaviour
{
	
	void OnGUI()
	{
		GUI.color = Color.white;
		GUI.Label (new Rect(Screen.width/2,Screen.height/2, 50, 50), "X");
	}
}

you want to change OnUpdate to just Update,

and you want to change ‘if (playerclose == true)’ to ‘if(playerclose.playerclose)’

  1. theres no need to check == true, since its already a boolean. if it == true, itll just pass
  2. You are referencing the script, not the boolean in the script

you may want to change the name of: ‘private checkplayerdist playerclose;’ to make it more clear.

It would be:

private checkplayerdist playerDistScript;

and

if(playerDistScript.playerclose)