Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

.Push Returns 'Push' is not a member of 'Array[]'.

Discussion in 'Scripting' started by JaceWoop23, Feb 21, 2017.

  1. JaceWoop23

    JaceWoop23

    Joined:
    Jan 14, 2017
    Posts:
    2
    Im still a newbie to this and i am trying to create an inventory and store each item into an array.
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. function Start () {
    4.    
    5. }
    6.  
    7. function Update () {
    8. var pickupItem = Input.GetKeyDown(KeyCode.F);
    9. var inventory : Array [];
    10.     if (pickupItem) {
    11.         inventory.Push(gameObject);
    12.     }
    13. }
    14.  
    the line "inventory.Push(gameObject);" returns the error "Assets/Scripts/Inventory Prototype.js(11,27): BCE0019: 'Push' is not a member of 'Array[]'. Also i would like to know how to find which gameObject your mouse is pointing to.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Array[] isn't the correct syntax, however you really should not use Array at all. It had a purpose in Unity 1.0 but should have been removed long ago. Use a generic List instead, then you can use List.Add.

    --Eric
     
    Ryiah likes this.
  3. JaceWoop23

    JaceWoop23

    Joined:
    Jan 14, 2017
    Posts:
    2
    Do lists work the same as arrays?
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    No, they work better.

    --Eric
     
    Ryiah and lordofduct like this.
  5. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Even using lists your going to have to do some more research and maybe some tutorials to get the exact item under your cursor when someone presses F. You are going to have to learn about colliders and raycasting. You can google Unity Raycasting or colliders to get started. There are a lot of Tutorials in the tutorials section of Unity, and some cover these topics.
     
  6. UnSpotibleShadow

    UnSpotibleShadow

    Joined:
    Jan 14, 2015
    Posts:
    38
    I love how that sounded in my head.

    anyway, Eric is right. Use Generic Lists.

    Code (Javascript):
    1.  
    2. function Update ()
    3. {
    4. var pickupItem = Input.GetKeyDown(KeyCode.F);
    5. List<GameObject> inventory = new List<GameObject>
    6.     if (pickupItem)
    7.     {
    8.         inventory.Add(gameObject);
    9.     }
    10. }
    11.  
    The nice things about list is that they are easier accessible, and easier to modify throughout the time.
    For info about the functions of list make sure you check out MSDN
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You're mixing C# with Unityscript (although not correct C# either ;) ); the syntax is "var inventory = new List.<GameObject>();".

    --Eric
     
  8. UnSpotibleShadow

    UnSpotibleShadow

    Joined:
    Jan 14, 2015
    Posts:
    38
    Just wrote it quickly, so excuse me if syntaxing is wrong :D