Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Android Matt · Jul 05, 2013 at 08:03 PM · instanceclassestype

Build Queue Logic In Turn-based Strategy

I have a simple build queue in my game - it is currently coded in two lists:

  1. One containing a list of all the units (e.g. footsoldiers, archers etc), and

  2. Another list with their respective build time remaining.

It seems to be easy enough to add and remove items from those lists simultaneously, but I am having a bit of a mental block regarding how to go from the point where it is recognized that a unit has been built, to actually instantiating it in game (or taking it from a ready-made stack).

Given that I have quite a long list of potential units, and my game is turn-based (so lightning fast response isn't necessarily the priority) I decided that I would approach this by making a "unit prefab" that can be programatically customized when the unit is built so that it represents the right thing.

I also made a script called unitStats that contains a class called UnitType, which contains various parameters (attack, defense etc). Within there I have created series of instances of that class (e.g. var footsoldiers = new UnitType) and then defined all the characteristics of each unit separately.

I thought that I could use the following logic:

  1. Update build queue and check if any unit has been completed.

  2. If a unit has been completed then call the BuildUnit function which will,

  3. ...create a unit based on the stats held in the appropriate instance of the class in unitStats

The BuildUnit function should take something as a parameter so that the right unit is built (e.g. BuildUnit(unitType: UnitType)) This is where I think I'm stuck - I want to use communicate the UnitType to the BuildUnit function, but I think I'm not quite getting it right.

I'm getting a null reference exception if I try to call BuiltUnit(unitStats.unitType), where I then want the BuildUnit function to use that to set unitType.name, unitType.attack etc..

I imagine I could do this with string and a switch statement, but I feel like I am just getting something simple wrong.

Any ideas?

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
1
Best Answer

Answer by iwaldrop · Jul 05, 2013 at 08:51 PM

You should always post code here if you have some and you're asking a specific code question. But you have the right idea. Your UnitType class should probably have a reference to the Prefab so that you can just instantiate whatever prefab is already set. You can make it serializable so that you can set the value in the inspecter, and then have a list of UnitTypes to choose from. This example is in c#, but you should get the idea:

 [System.Serializable]
 public class UnitType
 {
     public GameObject prefab;
 }
 
 public UnitType[] unitTypes;
 
 void BuildUnit(UnitType unitType)
 {
     Instantiate(unitType.prefab, position, rotation);
 }
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 Android Matt · Jul 07, 2013 at 11:41 PM 0
Share

Thanks!

$$anonymous$$att

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

16 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Benefit of nested classes? 1 Answer

Polymorphism question for my custom class 1 Answer

Creating a data type instance difference question 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 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