• 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 /
avatar image
0
Question by xylofiso · Dec 26, 2015 at 03:31 AM · playerbooleanmultipledialoguefindobjectoftype

Altering a variable from multiple scripts

I am trying to be able to change a bool value from multiple scripts, but when I tried it made things stop working. A quick breakdown, I have a PlayerController script which controls the movement of the player, and also has a bool called "canMove", and must be true for the player to be able to be moved.

In a TextBoxManager script I have called the PlayerController script with "public PlayerController ness;" then in Start() "ness = FindObjectOfType();" and when a textbox is enabled "canMove = false;".

This worked fine, until I created a new script MenuController and tried calling the PlayerController script the exact same way, and would make "canMove = false" whenever the key to bring up the menu was pushed down.

Now my character is unresponsive to player input when the menu is called, but when talking to a NPC and the dialogue box appears, the player is freely able to move around (the TextBoxDialouge script is not setting "canMove = false" anymore). Is this not allowed in C# to call the FindObjectOfType in multiple scripts?? Or should I be referencing the script in another way? I eventually want the dialogue box and menu to be in the same graphic, but baby steps, I'm still very new to game design and programming.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by allenallenallen · Dec 26, 2015 at 04:43 AM

Well, the problem probably isn't where you disable movement but when you enable it from 3 different scripts. Say when you have the menu open AND talking to NPC, what happens when you exit the menu? I'm guessing the menu script will try to enable movement even when the NPC condition is still not met.

So to solve this problem, I think you should manage this movement thing in a completely new script or in the player movement script.

This script will check for ALL the conditions for enabling movement and return a boolean or something. The player script will then check the returned boolean to see if the player can move or not.

For example:

 bool CanPlayerMove() {
     if (MenuOpen || NPCTalk || Other Conditions)
         return false;
     else 
         return true;
 }
Comment
Add comment · Show 4 · 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 xylofiso · Dec 26, 2015 at 05:44 AM 0
Share

All player movement is handled in the PlayerController script. This script contains a bool called "can$$anonymous$$ove" and based on certain requirements in other scripts, such as $$anonymous$$enuController script, inside the actual $$anonymous$$enuController script, the PlayerController script is referenced then the "can$$anonymous$$ove" bool is switched.

Here is a snippet of the $$anonymous$$enuController script which is adjusting the bool "can$$anonymous$$ove" in the PlayerController script:

         if (paused)
         {
             ness.can$$anonymous$$ove = false;
             $$anonymous$$enuUI.SetActive (true);
             menuOpen = true;
             Time.timeScale = 0;
         }

Now also I have a TextBox$$anonymous$$anager script which adjusts the same bool from PlayerController as such:

 public void EnableTextBox()
     {
         if (!menu.menuOpen) 
         {
             textBox.SetActive (true);
             isActive = true;
             ness.can$$anonymous$$ove = false;
             StartCoroutine (TextScroll (textLines [currentLine]));
         }
     }

The $$anonymous$$enuController changes the bool for the player, but the TextBox$$anonymous$$anager no longer adjusts that bool. I sort of understand what your offering, and I might try and mess around with that idea, but is this just something not able to be done with the C# language since ultimately two scripts are trying to edit the contents of one script simultaneously? $$anonymous$$aybe if I reference the other scripts in PlayerController and set up the bool as a function like you suggested, this might work, but even if it did, I'm still not understanding the logic of the engine or object-orientated program$$anonymous$$g.

avatar image xylofiso · Dec 26, 2015 at 06:16 AM 0
Share

Attempting to rewrite the PlayerController script with your logic has solved this, but I still am not sure why the original code would not produce the desired outcome. Do you have anymore information on how a script can only be called upon by one other script? I will wait for a day or two to see if there are anymore responses that explain a little further before I submit your response as an answer, as I would appreciate a little more clarity on what is actually going on here.

This is how the addition I've made to the PlayerController script to get the player movement disabled in both situations:

 if (menu.menuOpen || textBox.isActive)
         {
             can$$anonymous$$ove = false;
         }
 
         if (!menu.menuOpen && !textBox.isActive) 
         {
             can$$anonymous$$ove = true;
         }
avatar image allenallenallen xylofiso · Dec 26, 2015 at 06:48 AM 0
Share

A script can be referenced and called by an unlimited number of other scripts. It all depends on how and when the variables are accessed.

Your original scripts should work... in theory. But I can't tell what's wrong when you only show snippets of code. I'm guessing somewhere in your code, you're setting can$$anonymous$$ove = true; when you shouldn't.

One other thing, stay away from Time.timeScale. http://docs.unity3d.com/ScriptReference/Time-timeScale.html

"When timeScale is set to zero the game is basically paused if all your functions are frame rate independent."

avatar image xylofiso · Dec 26, 2015 at 06:16 PM 0
Share

Yeah heh, I had to end up taking out the Time.timeScale due to it freezing the Update(), now got to look for a similar solution to freezing the enemy movements while the character is involved in dialogue or menu. Thanks though @allenallenallen, there wasn't any extra settings of the Boolean, and I also had a similar problem with trying to have multiple scripts call upon on script, so I thought something was amiss. Anywhoo, cheers, and thanks again!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

33 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Detecting if 2 Buttons are pressed at the same time 3 Answers

GUI Pop-Up On Cube Collision 1 Answer

Talking to multiple NPC's 2 Answers

auto run key 1 Answer

Distance from tagged objects problem 1 Answer


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