• 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 SamTheT · Feb 25, 2015 at 10:57 AM · javascriptarrays

JavaScript array.push() doesn't seem to work

So, I'm trying to make an array for enemies in my small game, but Unity doesn't like array.push().

Here's the code:

 #pragma strict
 
 var Health : float = 100;
 var positionX : int;
 var positionY : int = 1;
 var positionZ : int;
 var enemyArray = [];
 var enemyArrayLength : int = 10;
 
 function Update()
 {
     //Destroy(gameObject);
     for(var i : int = 0; i < enemyArrayLength; i++){
     enemyArray.push(enemyCreate());
     Debug.Log("i spawn an enemy");
     }    
 }
 
 function ApplyDamage (PlayerDamage : int)
 {
     Health -= PlayerDamage;
     
     if(Health <= 0)
     {
         Destroy(gameObject);
         //enemyArray.pop();
     }
 }
 
 function enemyCreate() 
 {
     coordMaker();
     var enemy = Instantiate(gameObject, Vector3(positionX, positionY, positionZ), transform.rotation);
 }
 
 function coordMaker()
 {
     var random : int = (Random.Range(1,3));
     if(random==1)
     {
     positionX = transform.position.x + Random.Range(1, 6);
     positionY = transform.position.y;
     positionZ = transform.position.z + Random.Range(1, 6);
     Health = 100;
     }
     else
     {
     positionX = transform.position.x - Random.Range(1, 6);
     positionY = transform.position.y;
     positionZ = transform.position.z - Random.Range(1, 6);
     Health = 100;
     }    
 }


Please help, guys, I don't know who to ask!

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 Baste · Feb 25, 2015 at 11:07 AM

You're using a built in array when you're doing this:

 var enemyArray = [];

Those are .NET arrays, and can't be resized, so they lack the .push method. Replace it with a "JavaScript Array":

 var enemyArray = new Array();

You also need to actually return an object from the enemyCreate method, otherwise things will go poorly:

 function enemyCreate() 
 {
     coordMaker();
     var enemy = Instantiate(gameObject, Vector3(positionX, positionY, positionZ), transform.rotation);
     return enemy;
 }

I'd suggest reading up on the two different array types here. Remember, Unity's "JavaScript" is NOT JavaScript. It's C# dressed up to look like JavaScript.

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 Hoeloe · Feb 25, 2015 at 11:20 AM 1
Share

It's not "C# dressed up to look like JavaScript", it's a variation of JavaScript called UnityScript, which is adapted to fit the Unity API.

Also, I recommend not using "Array", but using Lists ins$$anonymous$$d. They're much more reliable. http://unity3d.com/learn/tutorials/modules/intermediate/scripting/lists-and-dictionaries

In general, if you need a collection of objects that doesn't change size, use a built-in array (that's using [] notation). If you need a collection of objects that can change size freely, use Lists. You should never use Unity's "Array" class, it's bad program$$anonymous$$g practice.

avatar image SamTheT · Feb 25, 2015 at 12:05 PM 0
Share

Baste, your method worked, enemies spawn, but AI doesn't to them, so they are just standing there, even though I'm near, but when I didn't use the array, they all with AI. Any suggestions? I'm noob with arrays :(

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Trying to populate an array with a GameObject 1 Answer

Cannot add Instaniated gameObjects to a GameObject[] using Javascript. Error: "NullReferenceException: Object reference not set to an instance of an object" 2 Answers

My array does not update when object is destroyed. How do I fix it? (java) 2 Answers

For values in array 2 Answers

Is this considered bad coding 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