Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
0
Question by mrsam993 · Feb 26, 2021 at 04:39 PM · script.booleaninstance

Changing a bool on multiple instances of the same script

I have multiple enemies that use the same script in a scene and this script has a bool that will reset the enemy positions whenever the player enters a new room. I need this bool to be updated from a script attached to the door but I need to change the bool to be true on every instance of the script so every enemy is affected. I only know how to reference the script by mentioning the object it is attached too but this will only affect one instance. So is there an easy way to change every instance at once? Thank you!

Comment
Add comment · Show 1
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 HoweToGaming · Feb 27, 2021 at 12:25 AM 0
Share

little tricky with no script

 public class YourBool : MonoBehaviour
 {
     public static bool variableName;
 
     void Start()
     {
         variableName = true;
     }
 }
 
 public class AnotherBool : MonoBehaviour
 {
     public bool anotherVariableName;
 
     void Update()
     {
         if (YourBool.variableName)
         {
             anotherVariableName = true;
         }
     }
 }
 

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by xibanya · Feb 27, 2021 at 07:13 AM

You could do away with the bool entirely and use event callbacks. Have your door have

 public UnityEvent onReset;

Then in the inspector you'd be able to add callbacks like you can on UI buttons. You could drag all your enemies into here, then when you need everything to reset, call

 onReset?.Invoke();

But maybe you don't want to drag your enemies into the slots in the inspector, or you instantiate new enemies at runtime. In that case, in your enemy script you could add

 private static event Action onReset;

and a method

 public static void InvokeReset()
 {
 onReset?.Invoke();
 }

if you don't have one already, add a method

 private void OnReset()
 {
 // your reset code here
 }

and then in OnEnable, add

 onReset += OnReset;

and in OnDisable add

 onReset -= OnReset;

Then whenever you want all of your enemies to reset, have your door script (or any script) call EnemyClass.InvokeReset();

Comment
Add comment · 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

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

146 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 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 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 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 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

[C#] Checking InstanceOf? 2 Answers

Boolean being chaned on new scene load 1 Answer

NullReferenceException: Object reference not set to an instance of an object ??? 1 Answer

How can I link a GameObject instance to a script variable in a Prefab using the editor? 1 Answer

Checking a bool's state from another script? 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