Quantcast
Channel: Latest Questions by Ranth
Viewing all articles
Browse latest Browse all 10

Adding gameObject to List of different script

0
0
I am creating a spaceship fighter RTS. I have two player fighter ships attacking two enemy capital ships. When a capital ship dies, I need it to inform the player's ship of the death so that the player's ships stop attacking it and move onto the next enemyship in their EnemyList script. The EnemyList script is attached to the parent gameObject of the player's fighter ships, and has two lists. One is called enemyList, which lists all the enemies the fighter has been commanded to attack. The second List is called 'objectsToRemove'. On each frame, when objectsToRemove is not null, the EnemyList script will remove any gameObjects in objectsToRemove from enemyList. Script 1 which calculates death. using UnityEngine; using System.Collections; using System.Collections.Generic; public class HealthScript : MonoBehaviour { public int health; public int FighterBullet; public int Fighter; public bool alive; private EnemyList adding; // Use this for initialization void Start () { alive = true; } // Update is called once per frame void Update () { } void OnTriggerEnter(Collider other) { //I KNOW THIS LOOKS HORRIBLY INEFFICENT AND WE SHOULD ONLY CALL THE DEATH CHECK AT END, BUT THE PLAYER UNITS DONT FIRE CONTINUALLY THEN if (other.gameObject.name == "FighterBullet(Clone)") { health = health - FighterBullet; Destroy(other.gameObject); if (health (); List died = adding.objectsToRemove; // LINE WITH ERROR died.Add(gameObject); } alive = false; //Destroy the ship after 5 seconds, giving plenty of time for the playerunits to remove it from their arrays Destroy(gameObject,5f); } } Script 2 that maintains the enemyList. using UnityEngine; using System.Collections; using System.Collections.Generic; public class EnemyList : MonoBehaviour { //The purpose of this script is to hold the units current enemy list. //This list used to be in the Attack.cs, but when the player goes into 3rd or 1st person mode we turn off Attack //At that point, the enemy list is no longer being updated. Therefore, we nee this script to be on constantly to update //the list of enemies the player is seeking to kill public List enemyList = new List(); public List objectsToRemove = new List(); public GameObject currentTarget; //This bool checks to see if the player selected new enemies to attack. If so, we need to sort and attack public bool newList; private bool needToSort; private Attack attackScript; // Use this for initialization void Start () { //Sets list to empty when the unit is instantiated enemyList = null; attackScript = gameObject.GetComponent (); objectsToRemove = null; needToSort = false; } // Update is called once per frame void Update () { //If the player commands the ship to attack new targets, we need to sort the list and then attack if(newList == true) { enemyList = SortList(enemyList); attackScript.AttackList(enemyList); newList = false; } //If we have a list of enemies if (enemyList != null) { //If we have been told some enemy has died if (objectsToRemove != null) { currentTarget = enemyList[0]; foreach(GameObject o in objectsToRemove) { //Remove all objects from enemylist, if one of those objects is the current target, need to resort the list if (o == currentTarget) needToSort = true; enemyList.Remove (o); } //need to clear this so we dont keep trying to remove the same object objectsToRemove = null; if(needToSort == true) { //Sort assuming there are still enemies in the list if(enemyList.Count > 0) { enemyList = SortList (enemyList); attackScript.AttackList (enemyList); needToSort = false; } } } if (enemyList.Count == 0) enemyList = null; } } //This sorts the enemy array based on distance to unit; public List SortList(List enemyList) { newList = false; //If multiple enemies in list, need to sort the list by distance. Basically compares the distance of the first item in array to next items. If a later item is closer, it becomes first item. if (enemyList.Count > 1) { for (int e = 1; e

Viewing all articles
Browse latest Browse all 10

Latest Images

Trending Articles





Latest Images