Can only be called from the main thread

“GetKeyInt can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don’t use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.”

I’m new to Unity and Scripting and I can’t really understand what is going on. I’m almost thinking that it has something to do with the fact that I’m working with 2 scripts interacting with each other.
Here’s a description of what I want to do with the two scripts:
-The first script Isn’t finished but what I wrote is supposed to have a bool that when you press “shift”, a renderer will be disabled on another Gameobject. However it’s not this script that disables the renderer. If the bool is true, an animation will be triggered.
-The second script is the one who detects if the bool from the first script is true, and if true, it will disable its own Gameobjects renderer.

here’s the first script:

using UnityEngine;
using System.Collections;

public class DolanBehaviour : MonoBehaviour {
	public GameObject Dolan;
	public bool Flashlight = Input.GetKey (KeyCode.LeftShift);


	
	// Use this for initialization
	void Start () {

		bool Flashlight = Input.GetKey (KeyCode.LeftShift);

		}
	
	// Update is called once per frame
	void Update () {
		if (Flashlight == true) {
			GetComponent<Animation> ().Play ("FlashlightOn");
		}
		if (Flashlight == true) {
		
		}

	
	}
}

Here’s the second script:

using UnityEngine;
using System.Collections;

public class DarknessBehaviour : MonoBehaviour {
	public Renderer darknessRend;
	public GameObject Dolan;
	private DolanBehaviour dolanFlashLight;

	// Use this for initialization
	void Start () {
		dolanFlashLight = Dolan.GetComponent<DolanBehaviour> ();
		darknessRend = GetComponent<Renderer> ();
		darknessRend.enabled = true;

	
	}
	
	// Update is called once per frame
	void Update () {
		if (dolanFlashLight.Flashlight == true) {
			darknessRend.enabled = false;
		}
	
	}
}

I’ve tried to do what the warning said and copied my bool into the “start” function, but then it gives another error about an unexpected symbol (public).
I should maybe have said this from the start :P, It let’s me run the game, but when the game plays, nothing is happening.

If you can find another better solution to my script, like, you can make a better one. Please write it down.

in the first script you’re using Input.GetKey to initialize Flashlight. that’s not allowed. only define it there as you’re doing the initialization in Start already.

I can see a few things that could be causing an issue, the first and most important is your boolean declaration on line 6 of the first script

public bool Flashlight = Input.GetKey (KeyCode.LeftShift);

change that to this:

public bool Flashlight;

and change your Update method to this:

 // Update is called once per frame
     void Update () {
         Flashlight = Input.GetKey (KeyCode.LeftShift);
         if (Flashlight == true) {
             GetComponent<Animation> ().Play ("FlashlightOn");
         }
         if (Flashlight == true) {

         }

Finally you are attempting to set the value of an already declared bool in your start method on line 13,

bool Flashlight = Input.GetKey (KeyCode.LeftShift);

This should be reduced to,

Flashlight = Input.GetKey (KeyCode.LeftShift);

Although i can’t be sure that this will solve your problem (but i’m pretty sure) it will at the very least get you moving in the right direction.