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.
Answer by hexagonius · Aug 28, 2015 at 05:51 PM
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.
What @hexagonius said. Also, don't you need to put the Get$$anonymous$$ey in Update? Otherwise you only get the value at the time the script is created/started. This might be okay if you're creating the script in response to something, but it's unusual enough it should be pointed out.
Answer by kornstar83 · Jun 08, 2017 at 01:55 AM
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.
I would also like to suggest that you make a change in your na$$anonymous$$g conventions, to easily differentiate between variables and functions it is customary for all functions to be capitalized and variable all start with a lower case, So
bool Flashlight;
would actually be
bool flashlight;
(lowercase f), and the same thing for
GameObject Dolan;
would be,
GameObject dolan;
Your answer
Follow this Question
Related Questions
How to fix these errors when launching a built game 0 Answers
hide object with mouse enter 0 Answers
How to disable raycast/groundchecker on OnTriggerEnter? 0 Answers
new scripts wont work, old scripts work fine. why? new scripts won't do ANY work. 0 Answers
How do you change UI Text to an int? 4 Answers