“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.