Get variable from seperate script.

Hi, I know this question is quite common but I’m a total newbie to C# I would like the variable isShowingMouse to be able to be changed from a second script so that I can disable the mouse when ever an object is clicked on. My mouse code works, I just can’t get the other script to interact with it. I get the error “An object reference is required to access non-static member MouselockC.isShowingMouse'" and the error "The name isShowingMouse’ does not exist in the current context”. Heres the code I have so far.

This is my mouse script that works

using UnityEngine;
using System.Collections;

public class MouselockC : MonoBehaviour {

	public bool isShowingMouse = true;
	void Start(){ 

	void Update()
		{ Fire(); }
		{ ToggleMouse(!this.isShowingMouse); }
	public void ToggleMouse(bool showMouse)
		Screen.lockCursor = !showMouse;
		Screen.showCursor = showMouse;
		this.isShowingMouse = !this.isShowingMouse;
	private void Fire()
		Transform cameraTransform = Camera.main.transform;
		Ray fireRay = new Ray(cameraTransform.position, cameraTransform.forward);
		RaycastHit hit;
		if(!Physics.Raycast(fireRay, out hit))
		{ return; }
		Debug.Log("Hit: " +;


And here is the script I am trying to use control the above script.

using UnityEngine;
using System.Collections;

public class Free_Mouse : MonoBehaviour {

	// Use this for initialization
	void Start () {
		GameObject teleport = GameObject.FindGameObjectWithTag("teleport");
	// Update is called once per frame
	void Update () {

		isShowingMouse = MouselockC.isShowingMouse;

		//hasForce = script.isShowingMouse(); 

		if(MouselockC.isShowingMouse == false)
			Debug.Log("Pressed left click.");

	void OnMouseDown(){
		Destroy (this.gameObject);
	    MouselockC.isShowingMouse = false;

Thanks so much for any help!

You are really close. Change line 10 in the second script to:

MouselockC mouselockC = teleport.GetComponent<MouselockC>();

Then change line 21 in the same script to:

 if(mouselockC.isShowingMouse == false)

MouselockC is the class or script. A script can be attachd to multiple objects. What you need is access to the specific instance of that script attached to a specific object. (called a component in Unity speak). You are getting the object, but you are not saving a reference to the specific component.