• 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 Nallelcm · Feb 27, 2013 at 03:55 PM · programmingstateoopglobal variables

Best way to maintain a list of selected objects? (Static Classes?)

I was wondering what the best way to maintain some selected options or game states. That doesn't really explain it very well. Let me give you an example.

Lets say I'm building an RTS game. Say have 2 buildings, a factory and a refinery. There are 2 gui buttons to reflect this. When I click factory it sets the 'buildingToBuild' variable to a GameObject of the factory. When I click on the ground to place the factory, it places the factory and then nulls the buildingToBuild variable. Now when I click on the building it sets the 'selectedBuilding' variable to the factory game object.

does that make sense? I would like the buildingtobuild and selectedbuilding to be accessible by all classes. I was thinking that I could have a 'GameVars' class that contained static variables.

Would this be an acceptable way to approach this problem or would this create more problems for me down the road?

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
0

Answer by jorashwil · Feb 27, 2013 at 04:16 PM

What I see is you needing a call reference. For example, I do a lot of JS. So at the bottom I'd add:
@Script RequireComponent (name) //This is the main controller
@Script AddComponentMenu (srclocation/"filename") // This is the script in question
You can have as many scripts attached as you like to each object. You need one main script for the building and then the 2 selection scripts your having issues with attached and called upon on selection. That's the simplest way I know how to explain it.

Comment
Add comment · Show 6 · 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 Nallelcm · Feb 27, 2013 at 04:21 PM 0
Share

Well, all my 'buildings' will have a script for themselves.

the factory and refinery both would inherit from a building class. In that building class I would have something akin to

     void On$$anonymous$$ouseDown()
     {
         if (GameVars.selectedBuilding == null)
         {
             GameVars.selectedBuilding = gameObject
         }
     }

I ideally would like to avoid adding more than one script to my prefabs.

avatar image jorashwil · Feb 27, 2013 at 05:02 PM 0
Share

Then I would suggest a src file just for those scripts. Add a loadscript line to call on those class scripts you need so you only need 1 script attached.

avatar image Nallelcm · Feb 27, 2013 at 05:51 PM 0
Share

Ok, but would that make a new instance of those scripts?

avatar image jorashwil · Feb 27, 2013 at 05:59 PM 0
Share

No, all your doing is saying when one var is true call "this" script, when that var becomes false call "that" script. That's what I like to do. Yeah, it means more scripts but it helps the game engine stay running fast and not slow down.

avatar image Nallelcm · Feb 27, 2013 at 08:58 PM 0
Share

Ok, so would this be similar to inheritance then? Would it be an alternative to inheritance?

Show more comments

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

10 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

Related Questions

Multiple Cars not working 1 Answer

An OS design issue: File types associated with their appropriate programs 1 Answer

Is there a way to create new GameObjects in your scene from constructors? 3 Answers

OOP architexture analysis 1 Answer

Organizing Enemy Movement Types 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