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 kievar1983 · Jan 01, 2012 at 10:00 PM · c#movementgameobjectenemy

c# array of game objects movement help

i am building a game that will have 2 arrays of game objects that will move on the screen. I can get the array to build the objects when told to.. i am having issues with looping through the array through another script and moving the objects.

i get alot of unexpected results such as null reference, or outside of range.

i'm sure it's something very simple i'm missin, i've tried for loops and foreach loops with no success.. i've even copied the array locally, but still didn't give me the correct results.

any and all help would be great on this.


Enemy Spawning script:

public int time = 0;

public int spawnTimer = 225;

public int trueCount = 0;

public static int waveCount = 10;

public static int waveCounter = 30;

public static int enemyCount = 0;

public GameObject enemyPrefab1;

public GameObject enemyPrefab2;

public static GameObject[] enemy1;

public static GameObject[] enemy2;

public bool created = false;

 // Use this for initialization
 void Start () {
     created = false;
     enemy1[trueCount] = Instantiate(enemyPrefab1,
                                     enemyPrefab1.transform.position,
                                     enemyPrefab1.transform.rotation)
                                     as GameObject;                    
     enemy2[trueCount] = Instantiate(enemyPrefab2,
                                     enemyPrefab2.transform.position,
                                     enemyPrefab2.transform.rotation)
                                     as GameObject;
 }
 
 // Update is called once per frame
 void Update () {
     if (time >= spawnTimer)
     {
         time -= spawnTimer;
         created = true;
         if (created == true)
         {
             if (trueCount < waveCount)
             {
                 enemy1[trueCount] = Instantiate(enemyPrefab1,
                                                 enemyPrefab1.transform.position,
                                                 enemyPrefab1.transform.rotation)
                                                 as GameObject;                    
                 enemy2[trueCount] = Instantiate(enemyPrefab2,
                                                 enemyPrefab2.transform.position,
                                                 enemyPrefab2.transform.rotation)
                                                 as GameObject;
                 trueCount ++;
                 enemyCount ++;
                 created = false;
             }
         }
     }
     else
     {
         time++;
         created = false;
     }
 }



movement script1:

public float speed = .25f;

public GameObject stopper;

public static float time = 0;

public static float timer = 15;

public GameObject[] enemies;

 // Use this for initialization
 void Start () {
     stopper = GameObject.FindGameObjectWithTag("EnemyDeath1");
 }
 
 // Update is called once per frame
 void Update () {
     foreach (GameObject enemy in Spawner.enemy1)
     {
         enemy.transform.position = enemy.transform.position - new Vector3 (0, 0, speed);
     }
 }



movement script2:

public float speed = .25f;

public GameObject stopper;

public static float time = 0;

public static float timer = 15;

public GameObject[] enemies;

 // Use this for initialization
 void Start () {
     stopper = GameObject.FindGameObjectWithTag("EnemyDeath2");
 }
 
 // Update is called once per frame
 void Update () {
     foreach (GameObject enemy in Spawner.enemy1)
     {
         enemy.transform.position = enemy.transform.position + new Vector3 (speed, 0, 0);
     }
 }
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 Winterblood · Jan 02, 2012 at 03:49 AM

 public static GameObject[] enemy1;

...is declaring a reference to an array of GameObjects that doesn't exist, because you haven't yet told it how big it should be. You need to create the array for it to reference before you can fill in any entries:

 void Start () {
     enemy1 = new GameObject[8];
 }

Also see this response for multidimensional arrays: http://answers.unity3d.com/questions/32746/unity-object-array-c.html#

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 kievar1983 · Jan 02, 2012 at 06:13 AM 0
Share

thanks this helpped, i can now get them all to instance properly and move across the screen..

i now have a new problem with the same scripts

in my line of:

foreach (GameObject enemy in Spawner.enemy1) { enemy.transform.position = enemy.transform.position + new Vector3 (speed, 0, 0); }

once i get 2, 3, 4 game enemies moving the speed is being applied to one enemy 4 times..

is there any way to stop this?

avatar image Winterblood · Jan 02, 2012 at 02:06 PM 0
Share

You'll find it easier if you work in a more modular fashion. Write a script that handles the movement of ONE enemy, and attach it to your prefab. Then that enemy can store his own speed, target, hit points or whatever, and has his own Update() function.

Ideally you'd just create the instances and leave them to it. When you create the instances you could fill in a reference to your spawner script so that each enemy can tell your spawner when it's about to die, and the spawner can clear that slot in it's array.

This is a popular program$$anonymous$$g philosophy called "Object-Oriented Program$$anonymous$$g" - keeping all the code for object X tucked away inside object X. That's how Unity is designed to work.

avatar image kievar1983 · Jan 02, 2012 at 04:54 PM 0
Share

thank you i completely forgot about the fact i can just make a game object and drop it int he inspector. i'm use to using XNA through visual studio where i use small lists of objects and update them all at once.

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

Making a bubble level (not a game but work tool) 1 Answer

Enemy stops patrolling when player comes close 2 Answers

Enemy moves randomly within a area. 1 Answer

Coroutine fires once after destroying gameobjects in a scene and recreating new objects 0 Answers

Unity how to make different enemy spawn one at a time 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