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 /
This question was closed Jul 21, 2014 at 01:33 PM by ado112 for the following reason:

Problem was fixed by myself.

avatar image
0
Question by ado112 · Jul 13, 2014 at 03:41 PM · noobclassesbegginermore

How to get object to have two or more same classes at script

Hi all,im totally new to classes and all that stuff so i want to make object to have more of same classes from one script based on one variable called enemyes:

 class classtut extends MonoBehaviour {
 
 var Enemyes : int; //this will declare how many classes we will have
 class Enemy {
 public var Health : float = 100;
 public var Obj : GameObject;
 public function En (hel : float){
 hel = Health;
 }
 }
 
 public var Enm : Enemy = new Enemy();
 
 function Start () {
 
 }
 
 function Update () {
 //number of enemyes = Enemyes
 Debug.Log(Enm.Health); //for the test
 //and other stuff...
 }                
 } //sorry for my bad english :(
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

  • Sort: 
avatar image
0

Answer by Tehnique · Jul 13, 2014 at 03:51 PM

 List<Enemy> AllEnemies = new List<Enemy>();


Then you just use:

 for(int i = 0; i < Enemyes; i++)
 {
     AllEnemies.Add(new Enemy());
 }

My code is C#, you'll have to turn it to Js, but you get the ideea.

Comment
Add comment · Show 9 · 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 ado112 · Jul 13, 2014 at 03:58 PM 0
Share

and how should that look like in my script,was that a C# code?

avatar image Tehnique · Jul 13, 2014 at 04:00 PM 0
Share

Yes, it's C#. Not a fan of Js, but I know it does not have a List, you have to use Array.

avatar image Tehnique · Jul 13, 2014 at 04:11 PM 0
Share

Probably something like:

 var AllEnemies = new Array ();
 
 for(var i : int = 0; i < Enemyes; i++)
 {
      AllEnemies.Add(new Enemy());
 }

As for your script:

 public var Enemyes : int = 6; //default value
 public var AllEnemies = new Array ();
  
 function Start () 
 {
    // Add 6 enemies.
    for(var i : int = 0; i < Enemyes; i++)
    {
       AllEnemies.Add(new Enemy());
    }
 }
  
 function Update ()
 {
   //print health for all enemies.
   for(var i : int = 0; i < AllEnemies.Length; i++)
   {
        Debug.Log(AllEnemies[i].Health);
   }
 
 }                
avatar image ado112 · Jul 13, 2014 at 04:40 PM 0
Share

turned everything into C#,now getting: Assets/Classtutc.cs(17,9): error CS0246: The type or namespace name List1' could not be found. Are you missing a using directive or an assembly reference?

avatar image Tehnique · Jul 13, 2014 at 04:55 PM 0
Share

You probably wrote something wrong. List declaration in C# is

 List<Enemy> AllEnemies = new List<Enemy>();

You probably named a list "List1" but you put that name where it was expecting the type (List).

Look at line 17, character 9 in your code, as the error says.

Show more comments

Follow this Question

Answers Answers and Comments

22 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

Related Questions

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

How can I move an object without translate or addforce? 3 Answers

Array of custom class objects all return the same value? 1 Answer

Best way to store and use player classes? 0 Answers

Recognizing a Mouse Click between Scripts 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