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
0
Question by Darky1 · Apr 11, 2013 at 02:11 AM · arraylistvariable

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;
Comment
Add comment · Show 3
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 Steven-1 · Apr 11, 2013 at 02:20 PM 0
Share

That's not a list, that's an array, array's cannot be extended

avatar image gheeler · Apr 11, 2013 at 02:22 PM 0
Share

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

avatar image Steven-1 · Apr 11, 2013 at 02:23 PM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

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 Darky1 · Apr 12, 2013 at 06:56 PM 0
Share

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

avatar image Eric5h5 · Apr 13, 2013 at 01:37 AM 0
Share

Don't use the Unity Array class; it's slow and obsolete. Use a generic List ins$$anonymous$$d.

avatar image
0

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);
         }
         }        
     
 }

 

}

Comment
Add comment · 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

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

16 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 avatar image avatar image avatar image avatar image avatar image

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

Accesing custom class object from another script 1 Answer

How to find the opposite variables list? 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