0
votes

I have a question. So I am using Unity to make a game, and I have run across a problem. When I try to run this script I get an error saying "An instance of type 'UnityEngine.Event' is required to access non static member 'keyCode'." I am not sure what to do. Thanks! This is the code that is having the error also.

 #pragma strict

var nothing = 0;


function Crosshair ()
{
    if(Event.keyCode == 27)
    {Screen.lockCursor = false;}
    else if (nothing)
    {Screen.lockCursor = true;}
}
2

2 Answers

0
votes

lockCursor is non-static, which means you need to make a new object and tell unity that it's a new instance of the class. Then you should be able to use that to call lockcursor. Not used to working with javascript, but in C# it would mean something like this in Javascript, I think:

var newEvent = new UnityEngine.Event();

newEvent.Screen.lockCursor = false; 
//and
newEvent.Screen.lockCursor = true;

Either that or

UnityEngine.Event newEvent = new UnityEngine.Event()

newEvent.Screen.lockCursor = false; 
//and
newEvent.Screen.lockCursor = true;

Hope it works for you!

0
votes

Actually you should use Event.current.keyCode instead of Event.keyCode. Simple.