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
0
Question by MonKcz · Jun 20, 2014 at 10:21 PM · arraysclassstatictower-defense

[SOLVED]From superior script to subordinate script

Hey guys,

today I have been working around this issue like half of a day. I would like to see some comparison between some methods. First I will try to explain you what did I do. I am developing Tower-defense game. Currently I am creating new tower system. My basic vision is this: 1 type of towers - 1 superior script. This script would contain every attribute of every upgrade level. (Sale price, price, range, firerate ...) and single towers (gameobjects) - tower script. This tower script would get attributes from superior script from the first instance of this. E.g. price of the first level. And then if someone press the button Upgrade on the same tower it gets again attributes from superior script but from the second instance. E.g. dmg of second level. I want to spare disk, memory space and performance so I figured out that I have to have one source and general script on every tower that can load everything from source. But I am wondering whats the best solution for this and certainly its not the GetComponent method. Trust me I tried it and it sucks. I figured out how to make it happen with static method and variables of the class in the source (superior script). Basically everytime player wants to build or upgrade the tower then it calls a static method of superior script with index. Next the method will load appropriate (by index) values to static variables. Last the variables of subordinate script will set their values based on static variables.

The question is: Is really neccessary to create a class with static variables or is better to use maybe static two dimensional array. One two-dimension array can represent one type of towers. I mean whats the best solution for this static solution? In comparison with speed, space usage and so on...

Btw. I will share here this tower system once it is done. (I guess in 2 days, I have no time tomorrow so..)

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 robertbu · Jun 20, 2014 at 10:36 PM

First your disk, memory and performance statement is misplaced. No matter what you do here, this code will take only a small fraction of all three compared to displaying one button, or one icon, or other small texture to the screen. A good design of this code will depend on the nature and implementation of multiple tower types. Given a single tower type, you can do something simple like this:

 public struct TowerEntry {
     public TowerEntry(float s, float p, float r, float f) {
         sale = s;
         price = p;
         range = r;
         firerate = f;
 
     }
     public float sale;
     public float price;
     public float range;
     public float firerate;
 }
 
 public static class TowerInfo  {
 
     private static TowerEntry[] towerData = new TowerEntry[] {
         new TowerEntry(1.0f, 1.1f, 1.2f, 1.3f),
         new TowerEntry(2.0f, 2.1f, 2.2f, 2.3f),
         new TowerEntry(3.0f, 3.1f, 3.2f, 3.3f),
         new TowerEntry(4.0f, 4.1f, 4.2f, 4.3f),
         new TowerEntry(5.0f, 5.1f, 5.2f, 5.3f),
         new TowerEntry(6.0f, 6.1f, 6.2f, 6.3f)
     };
 
     public static TowerEntry GetData(int level) {
         if (level >= towerData.Length || level < 0)
             level = 0;
 
         return towerData[level]; 
     }
 }

You would access the data for any given level by:

 TowerEntry te = TowerInfo.GetData (2);
 Debug.Log (te.range+","+te.price);

Note that nothing here is derived from MonoBehaviour, and that you access the info with the class name (no instance required).

For multiple tower types, you might want to derive all your towers from a base class, and have a static array as part of each individual derived class. That way each tower type would know how to upgrade itself.

Comment
Add comment · Show 2 · 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 MonKcz · Jun 21, 2014 at 08:13 AM 0
Share

Thank you, this is really smooth solution and clever. I will try it asap and let you know. $$anonymous$$uch appreciated :)

avatar image MonKcz · Jun 22, 2014 at 07:22 AM 0
Share

This works perfectly, really good job :) I owe you one.

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

2 People are following this question.

avatar image avatar image

Related Questions

Class for spawning units (gameobjects) 1 Answer

Problem setting up an array with a size determined by a variable 2 Answers

Error Assigning GameObject To Var Inside A Class 3 Answers

Auto Commenting lines 1 Answer

Error when trying to set variable with array 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