- Home /
Adding and Removing to a Inbuild Array
I have the class RespawnList
class RespawnList {
var player : NetworkPlayer;
var time : float;
}
And used it as a list
var respawnList : RespawnList[];
Now I want to add a RespawnList too respawnList
respawnList + temprespawnList;
But I get this error
Operator "+" cannot be used left hand side of type "RespawnList[]" and a right hand side of type "RespawnList"
How do I add temprespawnList to respawnList ? And how do I remove it ?
EDIT
temprespawnlist is
var temprespawnlist : RespawnList;
temprespawnlist.player = callInfo.sender;
temprespawnlist.time = Time.time + respawnTime;
That's not a list, that's an array, array's cannot be extended
create a new array, 1 longer than the previous one. copy all the contents in and then the new item into the last position in the array
Also those are very confusing (bad) names; your respawnList isn't the same type as temprespawnList, as you respawnList is an array of RespawnLists while your temprespawnlist is a single RespawnList
Answer by Sisso · Apr 11, 2013 at 02:26 PM
From docs "Builtin arrays (native .NET arrays), are extremely fast and efficient but they can not be resized."
http://docs.unity3d.com/Documentation/ScriptReference/Array.html
Option A. You must use a resizable collection.
var array = new Array();
array.Add("yeah");
Option B. Temporary move to a resizable collection.
var array = ["one", "two"];
var ar = new Array(array);
ar.Add("tree");
array = ar.ToBuiltin(String);
Option C. Resize the array
var array = ["one", "two"];
System.Array.Resize.<String>(array, array.length + 1);
array[array.length-1] = "tree";
Option D. Be creative
PS: I write now by hand, don't expect everything to compile
When I tryed to get the respawnTime out of one part of the array it gave me the error that "respawnTime is not a part of object"
I fixed it bye putting the part into a new variable.
var variable : ownclass = array[x];
Thank you for your anwser
Don't use the Unity Array class; it's slow and obsolete. Use a generic List ins$$anonymous$$d.
Answer by Steve_O · Apr 11, 2013 at 03:57 PM
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 < Targets.Count -1)
{
TargetIndex++;
}
else
{
TargetIndex=0;
}
DeSelectTarget();
SelectedTarget=Targets[TargetIndex];
}
//SelectedTarget.transform.FindChild("Info").GetComponent().text= SelectedTarget.name;
}
private void SelectTarget() { //SelectedTarget.transform.FindChild("Info").GetComponent().text= SelectedTarget.name;
}
private void DeSelectTarget() { //SelectedTarget.transform.FindChild("Info").GetComponent().text= ""; SelectedTarget.tag = "Ship"; }
void Update () {
//check if missle has set flag to true
if (MissleTarget.HasHitTarget == true)
{
ObjectToDestroy=MissleTarget.CurrentTarget;
Targets.Remove(ObjectToDestroy);
UpdateGUI();
}
//check if enemy has set flag to true
if (Enemy.HasHitTarget == true)
{
ObjectToDestroy=MissleTarget.CurrentTarget;
Targets.Remove(ObjectToDestroy);
UpdateGUI();
}
//fire laser when w pressed
if(Input.GetKeyDown("w"))
{
Instantiate(LaserSlot,transform.position,transform.rotation);
}
//scroll through targettable objects in list
if(Input.GetKeyDown("t"))
{
if(Targets.Count == 0 )
{
GetAllTargets();
}
else
{
TargetEnemy();
}
}
//fire missle at cuurently selected target
if(Input.GetKeyDown("e"))
{
if(Targets.Count >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);
}
}
}
}
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Is there a way to organize tracking of collisions? 0 Answers
Sorting Variables Help 1 Answer