Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by TrailerFilms1 · Aug 22, 2015 at 08:35 PM · scripting problemproblem during runtimescritpingtwomain thread

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dave-Carlile · Aug 28, 2015 at 06:15 PM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kornstar83 · Jun 08, 2017 at 02:02 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges