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 Seventh Stealth · Mar 12, 2015 at 09:25 PM · javascriptarrayenemy

How can an object add itself to a scripts Array when it's instantiated?

I have a manager script that manages all the enemies in my scene. To do this it has an array called EnemyObject where it manages all the objects at once. How can I have the enemy add itself to the EnemyObject array when its instantiated?

Thanks Stealth

Comment
Add comment · Show 1
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 Owen-Reynolds · Mar 13, 2015 at 12:42 AM 0
Share

Thinking about Instantiate just makes it more complicated. Yes, you Instantiated it, but now it's a gameObject, same as any other. You just have to write code to add it to a list.

I add things to arrays all the time. Lists work too. If you just focus on "add something to a list/array" you'll find lots of examples.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by asimov · Mar 12, 2015 at 09:37 PM

You need to make it a list instead of an array as this makes adding objects much easier, using .Add(). For each of the new enemies to be able to access the list, you need to make it a static variable (E.g. Inside the Manager script, when declaring the list, use public static List< GameObject> = new List ();) You'll need to add the System.Collections.Generic and System.Linq library headers to use Lists and Add(), respectively.

Then, in order for each new enemy to add itself to the list, you should do the work inside their Start function. Something like EnemyObjects.Add(gameObject);

This is untested but should work. Let me know how you get on.

Comment
Add comment · Show 3 · 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 Seventh Stealth · Mar 12, 2015 at 09:40 PM 0
Share

Do lists work in Javascript?

avatar image asimov · Mar 12, 2015 at 09:48 PM 0
Share

Yes they do work but with slightly different syntax.

  public var myList : List.<TYPE>; // Insert TYPE
avatar image Seventh Stealth · Mar 13, 2015 at 09:55 PM 0
Share

Apparently they lists dont work in Javascript so im going to keep on using arrays. Thanks for the help anyway

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

21 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

Related Questions

Physics.OverlapSphere with a min radius? 1 Answer

Enemy moving sideways 1 Answer

Checking Enemies In An Area. 1 Answer

Why does my pathfinding script keep crashing Unity? 1 Answer

Javascript - find a value in an array 0 Answers


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