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 code-blep · Oct 18, 2012 at 09:25 AM · arrayinspectorobjectssetup

Trying to be more efficient. Advice please...

Hi,

I have a script which sets up values for different Enemy Craft and passes the variables onto a spawned prefab, etc. An example of the variable structure is a bit like this:

 Enemy 1-----|Health
             |Turn Speed
             |Move Speed
             |Fly Mode

  //Not actual code but a rough example of initial Var setup.
 //The actually script is MUCH bigger, but this is an accurate representation of the structure at the moment.
     var enemy1Prefab : Transform;
     var enemy1Health : int;
     var enemy1TurnSpeed : float;
     var enemy1MoveSpeed : float;
     var enemy1FlyMode : boolean;


Everything works great. However, each time I want to add a new enemy, I have to copy/paste/modify chunks of code. Currently I am at 5 enemy and the script is getting huge. This got me thinking that there must be a more efficient way to do this and save time / reduce the amount of errors created with lots of editing. I am thinking an array is probably the best way to go...

I have recently learnt about List arrays, but I am not sure if they are the right way to go. Here are some specific questions:

1 - My goal is to be able to say in the Inspector that I want to create 10 enemies, when I do that 10 slots are created where I can enter the variable values for health etc. Is this the right direction to go?

2 - If an array is the way to go, am I looking at what is called a multidimensional array? Any advice on best practices for this type of thing?

3 - I am even considering about trying my hand at creating a custom editor (http://docs.unity3d.com/Documentation/ScriptReference/Editor.html). Is this overkill for my needs?

Hopefully someone can put me on the right path to being more efficient!

Thanks

Paul

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

Answer by Seth-Bergman · Oct 18, 2012 at 10:16 AM

What you can do is to create a custom struct or class which contains all of the data members you need.. Then, you can create ten instances of your struct, and each one can be tweaked individually. Here's how it would look:

 class enemy
 {
     var myPrefab : Transform;
     var myHealth : int;
     var myTurnSpeed : float;
     var myMoveSpeed : float;
     var myFlyMode : boolean;
     // ... etc
 }
 
 var enemy1 = new enemy();
 var enemy2 = new enemy();
 // ... etc

each instance you create (enemy1, enemy2, etc) will have its own complete set of the vars... Then, when you need to add a new enemy, just add another instance..

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 code-blep · Oct 18, 2012 at 11:38 AM 0
Share

Hi Seth, Thanks for the suggestion! I've not used Class before but this looks like a perfect time to give it a go. I'll have a play this evening, and will reply / vote after :)

I'm still open to other suggestions / techniques before I give Class a go, so anyone else feel free to chip in.

avatar image code-blep · Oct 20, 2012 at 02:30 PM 0
Share

Seth that was exactly what I looking for! Spent a few days on it and I love what classes can do. It saves so much time. Thanks for the help.

Paul

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

11 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

Related Questions

How do I make a public array of arrays appear in the inspector? 1 Answer

Multidimentional Array for Inventory style system in inspector. 1 Answer

Arrays and the inspector 1 Answer

Array not updating in inspector 0 Answers

How to make a custom type's contents modifiable in an array by inspector? 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