Quantcast
Channel: Answers for "Adding and Removing to a Inbuild Array"
Viewing all articles
Browse latest Browse all 4

Answer by Steve_O

0
0
Here is a copy of my C# script I used and this uses a list to store enemy ships nearby and you can remove the object from the list using UnityEngine; using System.Collections; using System.Collections.Generic; using System .Text; public class TargettingSystem : MonoBehaviour { public List Targets; public GameObject SelectedTarget; public int TargetIndex = 0; public GameObject Explosion; public GUIText Enemies; public GameObject Missle; public static bool DoDestroyTarget; public static GameObject ObjectToDestroy; public GameObject LaserSlot; public Texture NonEnemyGUI; public Texture EnemyGUI; private Vector3 ScreenCoords; // Use this for initialization void Start () { GetAllTargets(); Missle.transform.Find("Missle"); } public void GetAllTargets() { //TargetIndex = 0; GameObject[] go = GameObject.FindGameObjectsWithTag("Ship"); foreach(GameObject enemy in go) { Targets.Add(enemy.gameObject); } TargetIndex = Targets.Count; SelectedTarget = Targets[0]; UpdateGUI(); } private void TargetEnemy() { UpdateGUI(); if(SelectedTarget == null) { SelectedTarget=Targets[0]; } else { TargetIndex = Targets.IndexOf(SelectedTarget); if(TargetIndex 0) { //create missle Instantiate(Missle,transform.position , transform.rotation); SelectedTarget.tag = "CurrentEnemy"; } } //Is selected target visible in the screen, if so get co-ords of target if(SelectedTarget.renderer.isVisible) { ScreenCoords = Camera.main.WorldToScreenPoint(SelectedTarget.transform.position); ScreenCoords.y = Screen.height - ScreenCoords.y; UpdateGUI(); } } public void UpdateGUI() { Enemies = GameObject.Find("HUDText").GetComponent(); Enemies.text = "No Of Targets:" + Targets.Count.ToString(); } public void OnGUI() { if (SelectedTarget.renderer.isVisible) { if(SelectedTarget.tag == "CurrentEnemy") { GUI.DrawTexture(new Rect(ScreenCoords.x-20f ,ScreenCoords.y-30f ,64,64),EnemyGUI,ScaleMode.ScaleToFit,true,0f); } else { GUI.DrawTexture(new Rect(ScreenCoords.x-20f ,ScreenCoords.y-30f ,64,64),NonEnemyGUI,ScaleMode.ScaleToFit,true,0f); } } } }

Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles





Latest Images