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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Array Checking

Discussion in 'Scripting' started by Mhmud-Fadel, Jul 30, 2016.

  1. Mhmud-Fadel

    Mhmud-Fadel

    Joined:
    Nov 13, 2013
    Posts:
    41
    Hi
    if we have an Array called theBits and need check two conditions:

    1-- when element destroyed, reduce theBits one element
    2-- when theBits has no elements, do something

    i tried using Array.Resize but there is an error !!
    please, I hope some guides
     
  2. SirNiklas

    SirNiklas

    Joined:
    Jun 7, 2015
    Posts:
    85
    You are simply using the wrong class. Array sizes are immutable so you need to create a new one everytime the first condition is active.

    The List<T> is a generic data container which allows you to add and remove items on the fly. You can access its elements like an array, with indexers.
    Code (CSharp):
    1.  
    2. [SerializeField]
    3. private System.Collections.Generic.List<Bit> theBits;
    4.  
    5. public void RemoveElement()
    6. {
    7.   theBits.Remove(0); // Removes the first element that got added
    8.    if (theBits.Count == 0) // The list is empty
    9.       DoSomething();
    10. }
    11.  
    12. private void DoSomething()
    13. {
    14. }
    If you add all the items to theBits at the initialization of this instance you could also use a Stack<T> to optimize removing the first item from the data container.
     
  3. Mhmud-Fadel

    Mhmud-Fadel

    Joined:
    Nov 13, 2013
    Posts:
    41
    it gave me this error:
    Code (CSharp):
    1. Assets/Scripts/target.cs(5,47): error CS0116: A namespace can only contain types and namespace declarations
     
  4. SirNiklas

    SirNiklas

    Joined:
    Jun 7, 2015
    Posts:
    85
    Well you need to paste the code I gave you into a class. Not just somewhere into a file.
     
  5. Mhmud-Fadel

    Mhmud-Fadel

    Joined:
    Nov 13, 2013
    Posts:
    41
    Sorry, I'm beginner, what Class did you mean ?
     
  6. SirNiklas

    SirNiklas

    Joined:
    Jun 7, 2015
    Posts:
    85
    Yeah it now seems obvious to me, probably the class you have in target.cs.
    I cannot help you further because you havent shown me any code at all.
     
  7. Mhmud-Fadel

    Mhmud-Fadel

    Joined:
    Nov 13, 2013
    Posts:
    41
    This is my code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [SerializeField]
    5. private System.Collections.Generic.List<Bits> theBits;
    6.  
    7. public class target : MonoBehaviour
    8. {
    9.  
    10.     public bool bitDestroy = false;
    11.     public GameObject[] theBits;
    12.  
    13.     private float targetSizeX;
    14.     private float targetSizeY;
    15.  
    16.     void Start()
    17.     {
    18.         theBits = GameObject.FindGameObjectsWithTag ("squareBit");
    19.     }
    20.  
    21.     void Update()
    22.     {
    23.         //StartCoroutine(Wait ());
    24.  
    25.    
    26.         targetSizeX = transform.localScale.x;
    27.         targetSizeY = transform.localScale.y;
    28.  
    29.     }
    30.  
    31.     void OnCollisionEnter2D (Collision2D other)
    32.     {
    33.         if(other.gameObject.tag == "squareBit")
    34.         {
    35.             //Debug.Log ("Excellent!");
    36.             Destroy(other.gameObject);
    37.             transform.localScale = new Vector3(targetSizeX + 1.0f, targetSizeY + 1.0f, 0.0f);
    38.             bitDestroy = true;
    39.         }
    40.     }
    41.  
    42. //    public IEnumerator Wait()
    43. //    {
    44. //        if(bitDestroy)
    45. //        {
    46. //            yield return new WaitForSeconds(1);
    47. //            bitDestroy = false;
    48. //        }
    49. //    }
    50.  
    51.     public void RemoveElement()
    52.     {
    53.         theBits.Remove(0); // Removes the first element that got added
    54.         if (theBits.Count == 0) // The list is empty
    55.             DoSomething();
    56.     }
    57.  
    58.     private void DoSomething()
    59.     {
    60.         Debug.Log("Hi");
    61.     }
    62. }
    63.  
     
  8. Errorsatz

    Errorsatz

    Joined:
    Aug 8, 2012
    Posts:
    555
    The new "theBits" should be inside the target class, replacing the array version. You'll have to adjust Start slightly to add the results to it. Also, the way you're using it, I don't think you need the SerializeField.

    Edit: What's going on with your Update function? You only need to set those at the time they get changed.

    Edit 2: If you're just removing an arbitrary element instead of the one that was collided with, you could probably just use a counter instead.