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

Accessing GameObject []

Discussion in 'Scripting' started by chethan-s, Aug 16, 2015.

  1. chethan-s

    chethan-s

    Joined:
    Aug 16, 2015
    Posts:
    5
    I am making Solitaire in unity and i need to access the cards in the slot above draw them to the slots below
    how do i remove an element from GameObject [ ] i need help on this topic in order to continue with my game.
     
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Setting the array element to null, for reference types, will "remove" the element from the array. Note that this doesn't change the fact that the array slot is still there- arrays have a fixed size that you define on initialization and which doesn't change (52 slots will always be 52 slots, unless you make a new array). So, you'll have a "null" there, meaning "no reference", but the slot will still be there.

    If you want a collection that can change size dynamically, look into "List" instead. With a list, you can use someList.Remove(someGameObject) or someList.RemoveAt(indexNumber) in order to completely remove that slot from the list.

    If you wish to swap places, you can do so by assigning the reference of ObjectA to a temporary GameObject variable, setting ObjectB to ObjectA's original array slot, then setting the temporary variable to ObjectB's original slot.
     
    Kiwasi likes this.
  3. chethan-s

    chethan-s

    Joined:
    Aug 16, 2015
    Posts:
    5
    Thank You Lysander i am planning to shift from Arrays to List, just out of curiosity if i sort the array i.e scan through the entire array find the index which is set to null move the next element to the index which is set to null and reduce the size of the array will that work? i know using List will solve the problem quite easily just wanted to know if i can try this as well.

    Thank You so much for taking time to reply to my question.
     
  4. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    You can't reduce the size of an array- it's created as a sequential block in memory- like "this variable type is 32 bytes, we're making a 10 slot array, so we need 320 bytes set aside for this array", and then variables that are "assigned" are copied or referenced in that initial memory space. This makes them VERY fast to iterate through.

    So, you can't shrink or grow an array, which means creating a whole new array and then copying the non-null elements over, but frankly it would be easier if whatever system you're using just null-checks and/or skips null references when iterating over it- also, if you keep all of your null at the back of the array by swapping them to the front whenever one is "removed", then you just loop over the array until any element reads "null", then break the loop.

    Using a list would also be 10x easier, since the size is dynamic.
     
  5. chethan-s

    chethan-s

    Joined:
    Aug 16, 2015
    Posts:
    5
    That's Correct, I Completely Agree with you, Why did it not occur to my mind when i started the project???
    Any ways i have this complete week to submit my assignment so will start implementing Lists instead of Arrays
    Once Again Thank You for providing all the information, I am very Grateful to you.
     
  6. chethan-s

    chethan-s

    Joined:
    Aug 16, 2015
    Posts:
    5
    usingUnityEngine;
    usingSystem.Collections;
    usingSystem.Collections.Generic;

    publicclassObject_Instantiator : MonoBehaviour
    {
    publicGameObjectbox;
    GameObject [] objs;
    List<GameObject> ext;
    publicMaterial[] mats;
    GameObjecttemp;
    inti;
    voidStart ()
    {

    bring_boxes_to_scene ();
    ext = newList<GameObject> ();
    }

    voidUpdate ()
    {

    }

    voidbring_boxes_to_scene()
    {

    objs = newGameObject[4];
    for (i = 0; i <objs.Length; i++)
    {
    temp = Instantiate (box, (transform.position + newVector3 (1 * i, 0.5f * i, 1 * i)), transform.rotation) asGameObject;
    temp.GetComponent<Renderer> ().material = mats ;
    objs = temp;
    }
    print (ext.Count);
    ext.Add (objs [1]);
    print (ext.Count);
    }
    }

    I was trying to experiment with lists but for some reason i can't add an element to list can you help me with this?
     
  7. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Recopy what you're doing over in [code ][/code ] tags with proper formatting, please.
     
  8. chethan-s

    chethan-s

    Joined:
    Aug 16, 2015
    Posts:
    5
    i got it working