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. Dismiss Notice

Resolved Enabling and disabling gameObjects using a list

Discussion in 'Scripting' started by Redo122_, Aug 11, 2020.

  1. Redo122_

    Redo122_

    Joined:
    Feb 17, 2020
    Posts:
    20
    Hello, I've been looking on google for a while and I can't figure out how to clear an array.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Arrays are of a fixed size so clearing it just means filling it with some default value. Unless you mean a List, in which case you can just call Clear() on it.
     
  3. Redo122_

    Redo122_

    Joined:
    Feb 17, 2020
    Posts:
    20
    how?
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Code (CSharp):
    1. for (int i = 0; i < myArray.Length; i++) {
    2.   myArray[i] = default;
    3. }
     
    Last edited: Aug 11, 2020
  5. Yanne065

    Yanne065

    Joined:
    Feb 24, 2018
    Posts:
    175
    Loop through the array and set each of the value to default/ null
     
  6. Redo122_

    Redo122_

    Joined:
    Feb 17, 2020
    Posts:
    20
    Gives me errors for line 1 upload_2020-8-11_10-4-1.png
     
  7. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Share your code?
     
  8. Redo122_

    Redo122_

    Joined:
    Feb 17, 2020
    Posts:
    20
    Code (CSharp):
    1.     void ClearAray()
    2.     {
    3.         for (int i = 0; i < gameObjectArray.4; i++){
    4.             gameObjectArray = <some default value>;
    5.     }
     
  9. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Try this:
    Code (CSharp):
    1.     void ClearAray()
    2.     {
    3.         for (int i = 0; i < gameObjectArray.Length; i++){
    4.             gameObjectArray[i] = null;
    5.         }
    6.     }
     
  10. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    You have to replace "<some default value>" with an actual value that's assignable to these elements.

    For example, if it's an array of GameObjects, it must be a value assignable to GameObject, either an actual instance of GameObject or the type's default, which is 'null'.

    Btw, you can also use built-in methods, such as:

    System.Array.Clear

    Here's an example with an integer array:
    Code (CSharp):
    1. var array = new int[] { 0, 1, 2, 3, 4, 5 };
    2. System.Array.Clear(array, 0, array.Length);
    Or write a generic extension method so that you can call it as if it were a member of an array.
     
  11. Redo122_

    Redo122_

    Joined:
    Feb 17, 2020
    Posts:
    20
    doesn't work
     
  12. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    I mean it certainly sets your whole array to null. Care to elaborate on what isn't working?
     
  13. Redo122_

    Redo122_

    Joined:
    Feb 17, 2020
    Posts:
    20
    It just doesn't seem to do that.
     
  14. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    How do you know? Are you printing out the contents?
     
  15. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    You need to share more information. Unless anyone here has clairvoyance, posts like "doesn't work" are pretty much useless.

    Seeing the whole script would help, along with some information on what you expect the script to do (and what it is incorrectly doing instead)
     
    Bunny83 likes this.
  16. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Bunny83, Redo122_ and bobisgod234 like this.
  17. thorham3011

    thorham3011

    Joined:
    Sep 7, 2017
    Posts:
    25
    You're trying to null the array elements without knowing if the array is of a nullable type. If it's an array of ints for example, then you can't null the array elements.
     
  18. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Well I originally told op to use the default keyword which would work no matter what. Then his array was revealed to be named "gameObjectArray" so I made an informed assumption.
     
    Yanne065 and mopthrow like this.
  19. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    I'm going to throw a random guess out here then. When you say you want to "clear" the array, do you mean you want its length to become 0 after removing all references?

    If so, arrays don't have that capability. Once you define an array with a length...
    Code (CSharp):
    1. object[] objectArray = new object[10];
    ...That length cannot be changed. You can make every reference in the array
    null
    , but it will always keep its length.

    You'd want to use a List, as mentioned above, which can have a dynamic length.
    Lists also do have a Clear method, which is probably what you're looking for. It will remove all references and set is length to 0.
     
    Redo122_ likes this.
  20. Redo122_

    Redo122_

    Joined:
    Feb 17, 2020
    Posts:
    20
    I've gotten it set up as a list (hopefully) by creating the list in my void Start
    Code (CSharp):
    1.     void Start ()
    2.     {
    3.         ArrayList LevelList = new ArrayList();
    4.         rb = GetComponent<Rigidbody>();
    5.         count = 0;
    6.         level = 1;
    7.         PickupCountUpdate ();
    8.         SetCountText ();
    9.         SetLevelText ();
    10.         winText.text = "";
    11.         ClearOtherLevels ();
    12.     }
    and adding "Level 2" in the ClearOtherLevels void.
    Code (CSharp):
    1.     void ClearOtherLevels ()
    2.     {
    3.         ClearAray();
    4.         if (level == 1)
    5.         {
    6.             LevelList.Add("Level 2");
    7.         }
    8.     }
    However unity is telling me that "The name 'LevelList' does not exist in the current context" on line 6 of void ClearOtherLevels. Also how would I go about disabling and enabling GameObjects with tags using a list?
     
  21. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Not an ArrayList, that's a separate thing and isn't as performant as a List.

    The reference to LevelList doesn't exist in ClearOtherLevels because you declared it in Start; it only exists inside the Start method. You'll want to declare it globally at the class-level, rather than locally at the method-level:
    Code (CSharp):
    1. public class Example : MonoBehaviour {
    2.    List<string> levelList = new List<string>();
    3.  
    4.    void ClearOtherLevels() {
    5.       levelList.Clear();
    6.    }
    7. }
    Assuming you already have a List of GameObjects, you'd loop through the List and enable/disable each GameObject in it:
    Code (CSharp):
    1. List<GameObject> objectsList;
    2.  
    3. void SetObjectsActive(bool active) {
    4.    for(int i = 0; i < objectsList.Count; i++) {
    5.       objectsList[i].SetActive(active);
    6.    }
    7. }
    Not entirely sure what relevance the tags of the GameObjects have in this context, unless you mean you want to enable/disable GameObjects in the List that have a specific tag?
    If so, you'd just add an additional check for the tag in the loop:
    Code (CSharp):
    1. List<GameObject> objectsList;
    2.  
    3. void SetObjectsActive(bool active) {
    4.    for(int i = 0; i < objectsList.Count; i++) {
    5.       if(objectsList[i].CompareTag("some-tag")) {
    6.          objectsList[i].SetActive(active);
    7.       }
    8.    }
    9. }
     
    Last edited: Aug 12, 2020
    Redo122_ and PraetorBlue like this.
  22. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    That is because you defined the array LevelList in the Start function, and as such that list only exists in the start function.

    If you want to access a variable from other functions in that class, it must be defined in the class and not a function.

    Code (CSharp):
    1. class MyClass
    2. {
    3.   ArrayList LevelList = new ArrayList();
    4.  
    5.   void ClearOtherLevels()
    6.   {
    7.     LevelList.Clear();
    8.       if (level == 1)
    9.       {
    10.         LevelList.Add("Level 2");
    11.       }
    12.   }
    13. }
    Note: I am using the .Clear() function instead here (since the ClearArray function is kind of redundant now you are using a List)

    Why did you choose an ArrayList instead of a regular old List?
     
  23. Redo122_

    Redo122_

    Joined:
    Feb 17, 2020
    Posts:
    20
    The tags were so that I could easily remove the gameObjects since I was using a different method before which I couldn't get working properly, hence this thread. I have them set up like this
    upload_2020-8-12_12-21-54.png
    so would it be easier to make a list of the level gameObjects? If so, would that be done with
    Code (CSharp):
    1. levelList.Add("Level 1");
    2. levelList.Add("Level 2");
     
  24. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    It would be easier to directly reference the GameObjects instead of the tags on the GameObjects to later get their references.
    No, that is simply a List of strings. You'd want a List of GameObjects instead:
    Code (CSharp):
    1. public List<GameObject> levelList;
    Making the List public (or private with the [SerializeField] attribute) will make it show up in the script's inspector. From there, you can drag-and-drop the level GameObjects into the List.
     
    Redo122_ likes this.
  25. Redo122_

    Redo122_

    Joined:
    Feb 17, 2020
    Posts:
    20
    I'm getting "error CS1031: Type expected" with this
    Code (CSharp):
    1.     public List<"Level 2"> levelList;
    Without the quotations I get "error CS1003: Syntax error, ',' expected"
    Code (CSharp):
    1.     public List<Level 2> levelList;
     
    Last edited: Aug 12, 2020
  26. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    You have to literally define it as:
    Code (CSharp):
    1. public List<GameObject> levelList;
    The
    GameObject
    inside the
    <>
    symbols specifies that this is a List that contains GameObjects.

    They're called Generics, and you can read more about them here:
    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/
     
    Redo122_ and bobisgod234 like this.
  27. Redo122_

    Redo122_

    Joined:
    Feb 17, 2020
    Posts:
    20
  28. Redo122_

    Redo122_

    Joined:
    Feb 17, 2020
    Posts:
    20
    oh, I'm getting "The type or namespace name 'List<>' could not be found on line 3
    Code (CSharp):
    1.     public class Example : MonoBehaviour
    2.     {
    3.         List<string> levelList = new List<string>();
    4.     }
     
  29. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    Why did you go back to using strings?

    You need to have a "using System.Collections.Generic" at the top of your script. List is part of that namespace.
     
    Redo122_ and Vryken like this.
  30. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    What @bobisgod234 said.
    Add that namespace at the top of your script, and define the List as a List of GameObjects, not a List of strings:
    Code (CSharp):
    1. using System.Collections.Generic;
    2.  
    3. public class Example : MonoBehaviour
    4. {
    5.    public List<GameObject> levelList;
    6. }
    Additionally, if you're using Visual Studio or any other IDE, you will be given automatic suggestions to fix errors like, "The type or namespace name 'List<> could not be found". In Visual Studio, click on the line that the error is pointing to and a little light-bulb icon will appear. Click that icon and it will give you an option to automatically add the
    using System.Collections.Generic;
    declaration at the top of the script.

    Many compiler errors can be fixed this way.
     
  31. Redo122_

    Redo122_

    Joined:
    Feb 17, 2020
    Posts:
    20
    After adding that I'm now getting "The name 'levelList' does not exist in the current context" on line 3
    Code (CSharp):
    1.     void ClearOtherLevels ()
    2.     {
    3.         levelList.Clear();
    4.         if (level == 1)
    5.         {
    6.             levelList.Add("Level 2");
    7.         }
    8.     }
     
  32. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    @Redo122_ , I think you would benefit from taking a step back and doing some basic C# tutorials just to get familiar with the programming language. While you may eventually slog your way through whatever you're trying to do here by posting all your errors in this thread for the next week, I don't think you'll actually learn anything this way.

    https://www.tutorialsteacher.com/csharp/csharp-tutorials
     
    Vryken, bobisgod234 and Redo122_ like this.
  33. Redo122_

    Redo122_

    Joined:
    Feb 17, 2020
    Posts:
    20
    Alright, I'll do that. Thanks everyone for your help.
     
    Vryken and PraetorBlue like this.
  34. thorham3011

    thorham3011

    Joined:
    Sep 7, 2017
    Posts:
    25
    Yeah, I missed that, sorry. Very strange that that code with the gameObjectArray doesn't work.