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

Question Cannot apply indexing with [] to an expression of type int

Discussion in 'Scripting' started by drsack, Sep 20, 2020.

  1. drsack

    drsack

    Joined:
    Sep 19, 2020
    Posts:
    5
    Im following a video tutorial on how to make a simple inventory system but I cant get it to work for some reason, the message that pops up is "Cannot apply indexing with [] to an expression of type int" can someone help?

    Here's the code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Runtime.InteropServices;
    4. using UnityEngine;
    5.  
    6. public class Inventory : MonoBehaviour
    7. {
    8.  
    9.     public GameObject inventory;
    10.     public GameObject slotHolder;
    11.  
    12.     private int slots;
    13.     private Transform[] slot;
    14.  
    15.     public void Start()
    16.     {
    17.         // slots being detected
    18.    
    19.         slots = slotHolder.transform.childCount;
    20.         slot = new Transform[slots];
    21.         DetectInventorySlots();
    22.  
    23.     }
    24.  
    25.     public void Update()
    26.     {
    27.  
    28.  
    29.     }
    30.  
    31.     public void OnTriggerEnter(Collider other)
    32.     {
    33.  
    34.  
    35.     }
    36.  
    37.     public void AddItem(GameObject item)
    38.     {
    39.  
    40.     }
    41.     public void DetectInventorySlots()
    42.     {
    43.        // This is where the problem is I think
    44.         for (int i = 0; i < slots; i++)
    45.         {
    46.             slots = slotHolder.transform.GetChild(i);
    47.             print(slot.name);
    48.         }
    49.     }
    50. }
     
    Last edited: Sep 20, 2020
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    First off, please use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    Second, error messages have line numbers in them. That's how you find the error. At the minimum, be so courteous when you post the above code and actually indicate which line that the error corresponds to in your posted code.

    Spoiler: the line numbers are often different between what prints here in the forum, and what prints in your IDE, so a nice and easy...

    // error happens in this next line!


    ... helps us all get on the same page.
     
  3. drsack

    drsack

    Joined:
    Sep 19, 2020
    Posts:
    5
    Sorry, its my first time.. I updated it though, and indicated where I think the problem is. Thank you for your help!
     
  4. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Code (CSharp):
    1. slots = slotHolder.transform.GetChild(i);
    "slots" is an
    int
    .
    Transform.GetChild
    returns a
    Transform
    .
    You cannot assign "slots" to a
    Transform
    reference.

    I'm guessing you may have made a mistake somewhere when following the tutorial.
    The name "slots" being given to an
    int
    , and "slot" being given to an array makes me believe you may have accidentally swapped the names of these two variables, since arrays are commonly plural-named.
     
    drsack likes this.