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 Ryanvanpolen · Jan 18, 2014 at 04:22 PM · arrayvariabledifferent

Make an array with different variables...

I've got a problem with making an array with different variables(for an enemy). I want something like this: (it's visual)

       What          Position          Lives          Level          type

       name           x,y,z              200            3              ground
       name           x,y,z              200            3              ground
       name           x,y,z              200            3              ground

How can i get this result. (i only know how to make one with one variables)...

Thanks...

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

3 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by YoungDeveloper · Jan 18, 2014 at 04:53 PM

You could make a class array and input those fields from inspector or hardcode those variables from script. I don't think saving position would be a good idea though, as you will most likely place mobs randomly across the map, so position for each of them would be different.

 [System.Serializable]
 public class Enemy{
     public string thisName;
     public Vector3 thisPosition;
     public int lives;
     public int level;
     public string type;
 }
 
 public Enemy[] enemyList;
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 YoungDeveloper · Jan 18, 2014 at 05:11 PM 0
Share

Also, cool down variables for each enemy should be saved on each enemy itself, same as health or else they will share single variable, which will result in crazy house.

But methods for attacking and other activity can be written once, in same script this class array is. So when you can attack you just call the right method, and pass variables if you need such.

avatar image
3

Answer by AlucardJay · Jan 18, 2014 at 04:55 PM

note : you havn't specified the programming language you use, all my examples are in uJS

Create your own class :

 public class EnemyClass
 {
     public var name : String;
     public var position : Vector3;
     public var lives : int;
     public var level : int;
     public var type : String;
 }

Then in your script, declare a class like this :

 var enemy1 : EnemyClass;

You can assign values through script :

 enemy1.name = "Marauder";
 enemy1.position = Vector3( 10, 0, 20 );
 enemy1.lives = 200;
 enemy1.level = 3;
 enemy1.type = "ground";

or simply assign them in the inspector

You can also make an arry of the enemy class :

 var enemies : EnemyClass[];

Try it out. Create a new script, attach it to an empty gameObject in a new scene. In the inspector, set the size of the enemies array, then fill in all the information :

 public class EnemyClass
 {
     public var name : String;
     public var position : Vector3;
     public var lives : int;
     public var level : int;
     public var type : String;
 }
 
 var enemies : EnemyClass[];
 
 function Start()
 {
     for ( var i : int = 0; i < enemies.Length; i ++ )
     {
         Debug.Log( "enemies[" + i + "].name = " + enemies[i].name );
         Debug.Log( "enemies[" + i + "].position = " + enemies[i].position );
         Debug.Log( "enemies[" + i + "].lives = " + enemies[i].lives );
         Debug.Log( "enemies[" + i + "].level = " + enemies[i].level );
         Debug.Log( "enemies[" + i + "].type = " + enemies[i].type );
     }
 }
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 AlucardJay · Jan 18, 2014 at 04:57 PM 0
Share

Wow, looks like I was late with this answer! Both Jamora and YoungDeveloper have given great examples.

Also :

Please don't post comments as answers. Post comments by clicking the [add new comment] button, a window then open for you to type in. Answer fields are for answers only, as this is a knowledge base.

Here at Unity Answers, Answer means Solution, not Response.

  • Read this page : http://answers.unity3d.com/page/newuser.html

  • Please watch : http://video.unity3d.com/video/7720450/tutorials-using-unity-answers

I have converted this one for you.

avatar image YoungDeveloper · Jan 18, 2014 at 05:00 PM 0
Share

But you still had to post the longest and most bad ass answer haha

avatar image Jamora · Jan 18, 2014 at 05:02 PM 1
Share

This is called Object Oriented Program$$anonymous$$g. There are lots of tutorials and information available on the Internet, but since we're on Unity Answers, I feel I should link to the Unity program$$anonymous$$g tutorials here. Beginner lesson 23 is most likely of particular interest to the OP.

avatar image AlucardJay · Jan 18, 2014 at 05:07 PM 0
Share

lol, sometimes I do try to help people (ins$$anonymous$$d of just telling people to format code, don't post comments as answers, etc etc) =]

avatar image Ryanvanpolen · Jan 19, 2014 at 02:48 PM 0
Share

Thank you very much guys...

Show more comments
avatar image
0

Answer by ProjectCryken · Jan 18, 2014 at 04:34 PM

I think 5 different arrays would be suitable, a string, a Vector3, two ints and another string array.

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 Ryanvanpolen · Jan 18, 2014 at 04:47 PM 0
Share

How can you attach all of them??? I want one array made of different arrays. How can i do that???

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

24 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

Related Questions

A node in a childnode? 1 Answer

Getting information from an array 1 Answer

How to make an array of Interactive Clothes in one variable 0 Answers

How to expose variables in c# according to selected booleans 1 Answer

Why can't I change another script's variable with this script? Thanks, 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