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 02, 2012 at 07:58 AM · c#gameobjectarrays

odd error in c# script when working with arrays

i'm working with arrays of game objects and when i set an array element to be destroyed i know it should goto a null value and i'm even skipping the null value elements (rather than rebuild and shrink the array down (which i'm thinking about doing.)

the error i get is:

transform.position assign attempt for 'enemy1(Clone)' is not valid. Input position is { 245.247147, 2.169722, -Infinity }. UnityEngine.Transform:set_position(Vector3)

i found a work around to set from center to pivot and this worked for the first element in the array but not for anything else.

so i guess i ask, how would i easily loop through an array of game objects and update a position and remove the element from the array when it reaches a border.

the array is being set in another script.

this is my current script:

 using UnityEngine;
 using System.Collections;
 
 public class movement1 : MonoBehaviour {
     public float speed = .25f;
     public GameObject stopper;
     public static float time = 0;
     public static float timer = 15;
     public GameObject[] enemies;
     public bool deathSub = false;
     public bool destroy = false;
     public int count = 0;
     // Use this for initialization
     void Start () {
         stopper = GameObject.FindGameObjectWithTag("EnemyDeath1");
     }
     
     // Update is called once per frame
     void Update () {
         if (MainGUI.stopped == false)
         {
             count = Spawner.enemyCount;
             if (Spawner.creating == true)
             {
                 foreach (GameObject enemy in Spawner.enemy1)
                 {
                     if (enemy != null)
                     {
                         if (destroy == false)
                         {
                             enemy.transform.position = enemy.transform.position - new Vector3 (0, 0, speed / (Spawner.enemyCount + 1));
                         }
                         
                         if (enemy.transform.position.z <= stopper.transform.position.z)
                         {
                             destroy = true;
                         }
                         if (destroy == true)
                         {
                               Destroy(enemy);
                             deathSub = true;
                             destroy = false;
                         }
                         if (deathSub == true)
                         {
                             Spawner.enemyCount--;
                             deathSub = false;
                         }
                     }
                     else
                     {
                         continue;
                     }
                 }        
             }
         }
     }
 }
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 syclamoth · Jan 02, 2012 at 08:54 AM 0
Share

Divide-by-zero error. Check your logic.

2 Replies

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

Answer by Statement · Jan 02, 2012 at 02:14 PM

transform.position assign attempt for 'enemy1(Clone)' is not valid.

Input position is { 245.247147, 2.169722, -Infinity }.

UnityEngine.Transform:set_position(Vector3)

Your error isn't from your arrays, but from your arithmetics.

 enemy.transform.position = enemy.transform.position 
                            - new Vector3 (0, 0, speed / (Spawner.enemyCount + 1));

If enemyCount ever becomes -1, then Spawner.enemyCount + 1 becomes 0. In that case you'll get z evaluating speed / 0 which is infinity. Check how you handle enemyCount and I think you'll be able to narrow down your bug.


How would i easily loop through an array of game objects and update a position and remove the element from the array when it reaches a border.

Either you should use a for loop instead of a foreach loop, and set the value to null or use Array.IndexOf (but it perform slower since it has to search for it).

 // For approach
 for (int i = 0; i < Spawner.enemy1.Length; ++i)
 {
     GameObject enemy = Spawner.enemy1[i];
 
     if (enemy outside border)
     {            
         Spawner.enemy1[i] = null;
     }
 }

 // Foreach approach (slower)
 foreach (GameObject enemy in Spawner.enemy1)
 {
     if (enemy outside border)
     {
         int index = Array.IndexOf(Spawner.enemy1, enemy);
         Spawner.enemy1[index] = null;
     }
 }
Comment
Add comment · Show 1 · 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 04:55 PM 0
Share

thanks, after some diligiant searching i realized i was hitting -1 some times, so i threw in a catch to stop that.

avatar image
0

Answer by roamcel · Jan 02, 2012 at 01:33 PM

Usually, when you have variable array dimensions, you use

 List

which is a type from the

 System.Collections.Generic

namespace;

And you declare it like:

 private List<Transform> mylist;

To populate it with

 mylist.Add(mytransform);

It's very useful since you can use lots of important functions on it like 'removeat', 'contains' and others (check msdn reference or just use mono's autocompletion), and you can actually use them like arrays too.

On the other hand, if you just want to use an array, you will have to copy its contents on a new array with modified dimension if you want to add or remove elements. There's no automatic way to do it (best approach is the '.Range' function).

Comment
Add comment · 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

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

NullReference when accessing GameObject in array (C#) 1 Answer

C# Check If Gameobject is within Collider 1 Answer

press e to speek -1 Answers

Two exact objects behaving differently... *scratches head* 0 Answers

AddComponent() causes a "trying to create a MonoBehaviour using the 'new' keyword" warning 2 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