• 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
Question by Brutalitywarlord · Jun 03, 2016 at 03:30 PM · c#2d gamegetcomponentboolean

Changing Variables on one script with multiple other scripts

I am trying to set up the switch to determine who's turn it is for a 2d-TBS game.

there is a script called BoardManager which constructs the board, and holds the following variables which determines who's turn it is and how many actions they got

PlayerTurn EnemyTurn PlayerActions EnemyActions

The Player script and the Enemy script had to be added to the "headquaters" Gameobject in order to make them function

each of them has a Boardmanager script added to it as well to make GetComponent() work

the problem im having is when one script is finished it's turn, it's supposed to set it's turn to false with boardmanager. X Player Turn = false; and then set the other script to true with boardmanager. X OtherPlayer turn true;

but through debugging i have realized that both the boardmanagers attached to each headquarters are independant from each other, but i can't remove them or else GetComponent() doesn't work,

I need a way to reference only one Boardmanager, and get rid of the BoardManagers on the Headquarters, any ideas?

Comment

People who like this

0 Show 2
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 NoseKills · Jun 03, 2016 at 11:08 PM 0
Share

If it can only be the player's turn or enemy's turn at any given time, you should only have 1 variable in 1 place that indicates that. A boolean has 2 values so 1 boolean can be used to describe whose turn it is perfectly. Having many variables to mark the same piece of information leads into this kind of problems.

Yor problem is here

The Player script and the Enemy script had to be added to the "headquaters" Gameobject in order to make them function. Each of them has a Boardmanager script added to it as well to make GetComponent() work

This tells me you are having trouble figuring out how to reference scripts and variables unless you use them in a specific way. GetComponent can be used on any gameObject in the scene so that shouldn't prevent you from attaching the scripts anywhere you like. When you know how to do that, you can avoid hitting problems like this.

@harko12 's answer is one way to do this; to have a singular script you can easily access from anywhere.

avatar image Brutalitywarlord NoseKills · Jun 06, 2016 at 12:44 PM 0
Share

The "Headquarters" objects are prefabs and are instantiated instead of being in the scene

does this affect anything?

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by harko12 · Jun 03, 2016 at 03:40 PM

This would be a good time to use a singleton class. That way the player and the enemy could both look at the same BoardManager.

The quick and dirty way to do that is to add a new variable to your BoardManager script, like this:

 public static BoardManager instance;
 
 void Awake(){
  instance = this; // when the script starts, the instance variable gets set to this instance of the script.  Because it is static, there will be only one
 }

then in your player and enemy code, to get the BoardManager you just say var manager = BoardManager.instance;

then they will be referring to the same one.

As I said, that is quick and dirty. IF you want to make it fancier, do a search for Singleton on here and I'm sure there will be other examples.

Comment
ninja_gear

People who like this

1 Show 0 · 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.

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

155 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Reading a var from variable parents 1 Answer

AddForce not working on Instantiated prefab 0 Answers

Unity GetComponent Script? 1 Answer

How to assign a value to a Text Variable during runtime? 0 Answers

Animation disappears after getComponent? 0 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