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 Bokaii · Nov 04, 2014 at 10:00 PM · multiplayeronlinezombies

Add Transform to array

How do I add a transform which is instantiated?

My problem is, I have some enemies. I they all have a script with a transform array in it. When I spawn in, I want to add my own transform to the enemies enemy array!

Code:

 ENEMY SCRIPT:
  public Transform[] Enemies;


 SPAWN IN SCRIPT:
  public GameObject Player; //Assign in unity
  
  void Start(){
      Instantiate(Player);
      //Add transform to Enemies[] which is in ENEMY SCRIPT
  }
Comment
Add comment · Show 2
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 Bokaii · Nov 05, 2014 at 09:28 AM 0
Share

I mean, add a transform from an object which is instantiated. :)

avatar image Uldeim · Nov 06, 2014 at 11:00 PM 0
Share

This question, on its own, is nowhere near detailed enough to get a meaningful answer. With the clarification below (as a comment to james_170482's answer) it becomes slightly more viable. Could you be more specific as to how you're spawning the enemies and populating their original array? Through the Editor?

It would also be appreciated if you could edit your question when making clarifications so that people don't have to scroll through all the comments in all the answers to figure out its current state.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Kiwasi · Nov 06, 2014 at 11:42 PM

Use a generic list instead, its better for this application

 public class Player : MonoBehaviour {
 
     public GameObject Player;
     public EnemyScript enemyScript;
  
     void Start(){
         GameObject clone = (GameObject)Instantiate(Player);
         enmeyScript.enemyList.Add(clone.Transform)
     }
 }
 
 public class EnemyScript : MonoBehaviour {
     public List<Transform> enemyList = new List<Transform();
 }


Comment
Add comment · Show 4 · 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 Bokaii · Nov 07, 2014 at 06:59 AM 0
Share

Is there no way to do this in an array? :)

avatar image b1gry4n · Nov 07, 2014 at 07:19 AM 0
Share

Lists are much easier to use. If you are planning on adding,removing, and changing the number of objects contained at runtime, use a list

avatar image Kiwasi · Nov 07, 2014 at 08:12 AM 0
Share

You can do an array if you like. The code is pretty similar. The problem with an array is arrays are of fixed size. You will have to specify the size in advance, and figure out which index you want to add the item to. List takes care of all of that for you.

Why are you so set on using an array? There is nothing an array can do that a list can't. Under the hood a list is an array.

avatar image Linus · Nov 07, 2014 at 08:44 AM 1
Share

http://wiki.unity3d.com/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use?

avatar image
0

Answer by james_170482 · Nov 04, 2014 at 11:07 PM

Try this Untested code;

 public Tranform[] myTransforms;
 public int NumberOfTransformsYouNeedToStore = 10;
 public Gameobject myGameobject;
 
 void Awake()
 {
     mytransforms = new Transform[ NumberOfTransformsYouNeedToStore ];
 }
 
 void Start()
 {
     Gameobject go = Instantite( myGameobject ), new Vector3( 0, 0, 0 ), Quaternion.identity) as Gameobject;
     myTransforms[ arrayPositionNumberBetween " 0 and NumberOfTransformsYouNeedToStore - 1"];
 
 }

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 Bokaii · Nov 05, 2014 at 09:31 AM 0
Share

$$anonymous$$y problem is, I have some enemies. I they all have a script with a transform array in it. When I spawn in, I wan't to add my own transform to the enemies enemy array!

fx:

ENE$$anonymous$$Y SCRIPT:

 public Transform[] Enemies;
 
 
 

SPAWN IN SCRIPT:

 public GameObject Player; //Assign in unity
 
 
 void Start(){
     Instantiate(Player);
     //Add transform to Enemies[] which is in ENE$$anonymous$$Y SCRIPT
 
 }






avatar image james_170482 · Nov 06, 2014 at 04:48 AM 0
Share

Do you need each enemy to hold an array of all other enemies, wouldn't an empty gameobject with a script holding one public array be better or am I misunderstanding again lol

avatar image Bokaii · Nov 06, 2014 at 02:59 PM 0
Share

All my enemies have an script which tells them to go to a target. I wan't them to randomly select a target from the array, which will contain me and all the other enemies.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Remove first element of array 1 Answer

Find closest transform 1 Answer

Add one on local z axis 3 Answers

Closest array element 1 Answer

Particle System Delay 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