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(){
ToggleMouse(false);
}
void Update()
{
if(Input.GetMouseButtonDown(0))
{ Fire(); }
if(Input.GetKeyDown(KeyCode.E))
{ 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: " + hit.collider.name);
}
}
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");
teleport.GetComponent<MouselockC>();
}
// 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!