Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Favonius · Dec 15, 2015 at 02:40 PM · collisionlistgame object

Deleting an object from a list of objects from another script.

Hi everyone,

So I have 2 scripts set up. The first script is the BaseScript which handles the creation of soldier objects that will defend that base. And I have a script to handle the behavior of each individual soldiers.

When I create the soldiers I placed them to a list of GameObjects called unit[ ] to keep track of how many soldiers are currently still there. If all the soldiers are taken out, it will allow this base to be taken over by the player. The condition for this is if unit.count == 0, then I can take out the base.

The problem I'm having now is when I delete a soldier object when it collides with the player, I can't remove it from the unit List of that specific base.

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

 public class BaseScript : MonoBehaviour {
 
     public GameObject bases;
     public GameObject tile;
 
     public GameObject enemy;
 
     LevelManager levelManager;
     GameObject levelAccess;
 
     public int health;
     public SpriteRenderer color;
 
     public List<GameObject> unit;
 
     // Use this for initialization
     void Start () {
         unit = new List<GameObject>();
         for (float i = 0; i < 10; i++)
         {
             float num = (i * 1) / 10;
 
             float angle = num * Mathf.PI * 2;
 
             float x = Mathf.Sin(angle) * 2;
             float y = Mathf.Cos(angle) * 2;
 
             Vector3 point = new Vector3(x, y, 0) + this.transform.position;
 
             Instantiate(enemy, point, Quaternion.identity);
             unit.Add(enemy);
             
 
         }
 
 
 
         Instantiate(tile, new Vector2(bases.transform.position.x, bases.transform.position.y), Quaternion.identity);
         //health = levelManager.health;
         color = GetComponent<SpriteRenderer>();
         //color.color = new Color(0f, 0f, 1f, 1f);
 
     }
 
     // Update is called once per frame
     void Update () {
     
     }
 
     void SpawnSoldiers()
     {
         for(int i = 0; i<10;i++)
         {
             float theta = (2 * Mathf.PI / 10) * i;
             float x = Mathf.Cos(theta);
             float y = Mathf.Sin(theta);
         }
     }
 
     void OnTriggerEnter2D(Collider2D other)
     {
         Debug.Log("List legth" + unit.Count);
     }
 }
 

SoldierScript using UnityEngine; using System.Collections;

 public class EnemyController : MonoBehaviour
 {
     SpriteRenderer color;
     public Transform target;
     public GameObject playerPos;
     public float moveSpeed;
 
     public PlayerController playerAccess;
     public BaseScript baseAccess;
     public int health;
 
     // Use this for initialization
     void Start()
     {
         playerAccess = GetComponent<PlayerController>();
         baseAccess = GetComponent<BaseScript>();
         //moveSpeed = 5.0f;
         color = GetComponent<SpriteRenderer>();
         color.color = new Color(0f, 0f, 0f, 1f);
     }
 
     // Update is called once per frame
     void Update()
     {
 
         
        
     }
 
     public void Move(Vector3 position)
     {
         transform.position = Vector3.MoveTowards(transform.position, position, moveSpeed * Time.deltaTime);
         Debug.Log("New Locatio " + transform.position);
     }
     void OnTriggerStay2D(Collider2D other)
     {
         //Debug.Log("Something entered!");
         if (other.tag == "PlayerUnit")
         {
             transform.position = Vector3.MoveTowards(transform.position, other.transform.position, moveSpeed * Time.deltaTime);
         }
     }
     
 
     void OnCollisionEnter2D(Collision2D other)
     {
         if(other.gameObject.tag == "PlayerUnit")
         {
             Destroy(this.gameObject);
             baseAccess.unit.Remove(this.gameObject);
         }
     }
 }


Currently I keep getting the Object is not referenced to an object error from the soldier script.

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 Salogeo · Oct 09, 2016 at 09:50 PM 0
Share

Hello, i know this is a old question but i'll answer it just in case

you're deleting the game object before you remove it from the list when you do that the piece of code wont get completed because the gameobject is already destroyed so ins$$anonymous$$d of

 Destroy(this.gameObject);
 baseAccess.unit.Remove(this.gameObject);

try

 baseAccess.unit.Remove(this.gameObject);
 Destroy(this.gameObject);


1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by OncaLupe · Dec 17, 2015 at 10:54 AM

 Instantiate(enemy, point, Quaternion.identity);
 unit.Add(enemy);

You're instantiating a prefab, then adding the prefab to the units list. You instead need to add the newly created enemy to it:

 GameObject newEnemy = Instantiate(enemy, point, Quaternion.identity) as GameObject;
 unit.Add(newEnemy);
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 Favonius · Feb 04, 2016 at 08:26 PM 0
Share

So I fixed my Instantiate code but it still wouldn't remove the object from the list. I keep getting the same error.

Is there something wrong with how my to remove the enemy object from the EnemyController class is set up?

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

Adding points to Collider as element of a list. 0 Answers

Can I detect a collision between two triggers on other objects 1 Answer

CharacterController and Instantiating an object with collider 1 Answer

Help with Destory a vechicle code. 1 Answer

How do I count Hits ?? 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