Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How do I remove the first object from this list?

Discussion in 'Scripting' started by switchluts, May 17, 2020.

  1. switchluts

    switchluts

    Joined:
    Mar 18, 2019
    Posts:
    45
    How do I remove the first object from this list?
    I want to always remove 0 if Player is greater than 3 but the problem is I don't know how to remove the first object in my list using code.
    Code (CSharp):
    1.   [SerializeField] List <GameObject> numberOfPlayers;
    2.     [SerializeField] GameObject grave;
    3.     // Use this for initialization
    4.     void Start () {
    5.         playerRigid = GetComponent<Rigidbody2D>();
    6.         box = GetComponent<BoxCollider2D>();
    7.         manageGame = FindObjectOfType<ManageGame>();
    8.         numberOfPlayers.AddRange(GameObject.FindGameObjectsWithTag("Player"));
    9.     }
    10.  
    11.     // Update is called once per frame
    12.     void Update()
    13.     {
    14.  
    15.         player = GameObject.FindGameObjectsWithTag("Player").Length;
    16.  
    17.         if (hasDied == false)
    18.         {
    19.             Move();
    20.         }
    21.          if (player >= 3)
    22.         {
    23.             numberOfPlayers.Remove(gameObject);
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
  3. switchluts

    switchluts

    Joined:
    Mar 18, 2019
    Posts:
    45
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Sounds like you're trying to use "Remove" instead of "RemoveAt".
     
    Kurt-Dekker likes this.
  5. switchluts

    switchluts

    Joined:
    Mar 18, 2019
    Posts:
    45
    Thank you so much
     
    Kurt-Dekker likes this.