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
1
Question by Flague · Aug 11, 2012 at 06:40 PM · gameobjecttransformarraylistadd

Keep adding targets to a list

Hello, I have a list on witch i have every enemy on the map sorted by its distance to the tower (it's a tower defense game) The problem is in the method i use to add those targets to the list, I can only use it once because i dont know how to clear the array inside it.

i've tried using ArrayName.Clear(); with no results, i can't either get the lenght of it i guess because it's a GameObject Array but not sure...

public void AddAllEnemies(){

     GameObject[] go = GameObject.FindGameObjectsWithTag("Enemy");

     foreach(GameObject enemy in go){
     
         AddTarget(enemy.transform);
         i++;
     }
 }

     public void AddTarget(Transform enemy){
         targets.Add(enemy);
 }

This is the most important part of the code witch i'll post at the end, when an enemy dies i am supposed to clear the list and then add every enemy on the map but when i do this, because i can't clear the "go" array i add the actual enemys and the previous ones, so i end up with the same list twice, but the main problem is that the enemy i killed is dead and therefor when i sort them by distance an error appears.

Bottom line i need to clear the previous GameObject array everytime i call the AddAllEnemies method or find a way to update my "targets" list.

Thanks to everyone who tries to help me, and i hope you can because i am a bit despered with this S:

using UnityEngine; using System.Collections; using System.Collections.Generic; //using System;

public class TowerStone : MonoBehaviour {

 public List targets;
 public GameObject projectile;
 public Transform target;
 private Transform myTransform;
 private float coolDown = 2;
 private float t;
 private int i = 0;
 private bool ready = true;
 private bool arrayOn = false;
 //private delegate Transform myDelegate(Transform t1, Transform t2);
 
 // Use this for initialization
 void Start () {
     myTransform = transform;
     float t = Time.time;
     
     AddAllEnemies();
     TargetEnemy();
 }
 
 // Update is called once per frame
 void Update () {
     
     TargetEnemy();
     
     float c = Time.time - t;
     if (c > coolDown && ready){
         t = Time.time;
         Fire();
     }
     

     /*if(Input.GetKeyDown(KeyCode.Y)){
         AddAllEnemies();
     }*/
 }
 
 void Fire(){
     GameObject arrow = Instantiate (projectile, transform.position + new Vector3(0, 3, 0), Quaternion.identity) as GameObject;
     arrow.SendMessage("SetTarget", target);
     
 }
 
 public void AddAllEnemies(){

     
     GameObject[] go = GameObject.FindGameObjectsWithTag("Enemy");

     
     foreach(GameObject enemy in go){
     
         AddTarget(enemy.transform);
         i++;
         
     }

 }
 private void SortTargetsByDistance(){
     
     
     targets.Sort(delegate(Transform t1, Transform t2){
         return Vector3.Distance(t1.position, transform.position).CompareTo(Vector3.Distance(t2.position, transform.position));
             });
     
 }
     
 public void AddTarget(Transform enemy){
     targets.Add(enemy);
 }
 
 private void TargetEnemy(){

         SortTargetsByDistance();
         target = targets[0];
         ready = true;

 }
 private void TargetDestroyed(){

     targets.RemoveAt(0);
     
     ready = false;    

     SortTargetsByDistance();

     print (target);
 }
 
 void AddMe(){
     
 }

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Seth-Bergman · Aug 11, 2012 at 07:46 PM

if I understand, you want to clear the GameObject[] "go", correct?

the line:

  GameObject[] go = new GameObject[];

or something like that, should clear it, so maybe like:

GameObject[] go = new GameObject.FindGameObjectsWithTag("Enemy");

not sure if it will work this easily though..

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 AnXgotta · Aug 12, 2012 at 04:32 AM 0
Share

@Flague: Seth is right, you could just create a new array which would then be empty. You could also just iterate through the array and pop them out of the array.

foreach(GameObject enemy in go){ go.Pop(); }

avatar image Loius · Aug 12, 2012 at 05:41 AM 0
Share

Yes, = new GameObject[] will clear it. But you should remove 'new' from the second line - new is just for when you're creating the new array yourself. Since FindBlahBlah creates the array,l you don't need 'new.'

avatar image
0

Answer by Flague · Aug 12, 2012 at 06:05 PM

Thanks for the answers, i solved it by declaring the array null every time i call the AddAllTargets method, the "Problem" is that i call it every frame on the update, can't just call it when one target dies don't know why but it doesn't matter even thought i think calling it every frame will make it laggy but meh, here is the code if anyone is interested (:

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class Targetting : MonoBehaviour {

 private float coolDown = 2;
 private float t;
 public List targets;
 public Transform target;
 public GameObject projectile;
 float dist = 100;
 float distToFire = 5;

 void Start () {
     
     
     AddAllTargets();
 
     
 }

 void Update () {

     AddAllTargets();
     if (target != null)
         if(dist < distToFire)
             Timing();

 }
 
 void AddAllTargets(){
     GameObject[] go = null;
     go = GameObject.FindGameObjectsWithTag("Enemy");
     targets.Clear();
     for(int i = 0; i < go.Length; i++){

     }

     foreach (GameObject enemy in go){
         targets.Add(enemy.transform);
     }
     
     SortTargetsByDistance();
     TargetEnemy();
     
     dist = Vector3.Distance(transform.position, target.position);

 }
 
 private void TargetEnemy(){
     if (targets.Count > 0){

         target = targets[0];

         }
     
     
 }

 private void SortTargetsByDistance(){
     
     
     targets.Sort(delegate(Transform t1, Transform t2){
         return Vector3.Distance(t1.position, transform.position).CompareTo(Vector3.Distance(t2.position, transform.position));
             });
     
 }
 void Fire(){
     GameObject arrow = Instantiate (projectile, transform.position + new Vector3(0, 3, 0), Quaternion.identity) as GameObject;
     arrow.SendMessage("SetTarget", target);
     
 }
 private void Timing(){
     float c = Time.time - t;
     if (c > coolDown){
         t = Time.time;
         if (target != null)
             Fire();
     }
 }
 

}

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 CoolCasualCat · Oct 23, 2015 at 09:58 AM 0
Share

is the for loop in the AddAllTargets() method still necessary? I have a code similar, and I am trying to solve a problem I have for my own script.

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

11 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

Related Questions

How to remove objects from a list ? 3 Answers

Can't add GameObjects to ArrayList 1 Answer

How do I check multiple gameObjects transform positions? 3 Answers

Disable All Transform In List 1 Answer

Instantiating from an object that's in an array 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