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

Cannot implicitly convert type 'UnityEngine.Transform[]' to 'int'

Discussion in 'Editor & General Support' started by SuperMD50, Jun 22, 2021.

  1. SuperMD50

    SuperMD50

    Joined:
    Feb 5, 2020
    Posts:
    7
    so i need help since im getting this error and im new to programming, so explaining this in that way might not help. i dont know what exact line it is but if i can get any help that will be greatly appreciated. also im following a tutorial so i dont know if there's anything wrong there as it is working fine there.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Inventory : MonoBehaviour
    6. {
    7.  
    8.     public GameObject inventory;
    9.     public GameObject slotHolder;
    10.  
    11.     private int slots;
    12.     private Transform[] slot;
    13.  
    14.     private GameObject itemPickedUp;
    15.  
    16.  
    17.     public void Start()
    18.     {
    19.         // slots being detected
    20.         slots = slotHolder.transform.childCount;
    21.         slots = new Transform[slots];
    22.         DetectInventorySlots();
    23.     }
    24.  
    25.     public void Update()
    26.     {
    27.  
    28.     }
    29.  
    30.     public void OnTriggerEnter(Collider other)
    31.     {
    32.         if (other.gameObject.GetComponent<Item>())
    33.         {
    34.             itemPickedUp = other.gameObject;
    35.             AddItem(itemPickedUp);
    36.         }
    37.     }
    38.  
    39.     public void AddItem(GameObject item)
    40.     {
    41.  
    42.     }
    43.  
    44.     public void DetectInventorySlots()
    45.     {
    46.         for (int i = 0; i < slots; i++)
    47.         {
    48.             slot[i] = slotHolder.transform.GetChild(i);
    49.         }
    50.     }
    51.  
    52. }
    53.  
     
  2. highlyinteractive

    highlyinteractive

    Joined:
    Sep 6, 2012
    Posts:
    116
    If you look at your full error:
    Assets/Inventory.cs(21,17): error CS0029: Cannot implicitly convert type 'UnityEngine.Transform[]' to 'int'


    First you'll see the file name (Inventory.cs) followed by two numbers which are the line & column.

    So looking at line 21 you can see the problem is with
    slots = new Transform[slots];


    The actual error you're getting is because
    slots
    is an integer, but you're trying to assign a Transform.

    There are other errors in your code, but see if you can solve them yourself ;)
     
  3. SuperMD50

    SuperMD50

    Joined:
    Feb 5, 2020
    Posts:
    7
    ok thank you
     
  4. SuperMD50

    SuperMD50

    Joined:
    Feb 5, 2020
    Posts:
    7
    ok so i did this '
    // slots being detected
    slots = slotHolder.int.childCount;
    slots = new int[slots];
    DetectInventorySlots();
    '
    but its giving me errors about identifers and having a ; expected
     
  5. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    You typo'd line 21.

    It should be

    Code (CSharp):
    1.         slot = new Transform[slots];
    2.  
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    Let me save you a LOT of time!

    How to do tutorials properly:

    Tutorials are a GREAT idea. Tutorials should be used this way:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation generally ends in disaster. That's how software engineering works. Every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly. Fortunately this is the easiest part to get right. Be a robot. Don't make any mistakes. BE PERFECT IN EVERYTHING YOU DO HERE.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!
     
  7. SuperMD50

    SuperMD50

    Joined:
    Feb 5, 2020
    Posts:
    7
    ok thank you for the support, i will try this later
     
  8. SuperMD50

    SuperMD50

    Joined:
    Feb 5, 2020
    Posts:
    7
    also isnt that the same thing?
     
  9. SuperMD50

    SuperMD50

    Joined:
    Feb 5, 2020
    Posts:
    7
    i mean, if i have an error, i always check the comments but i didnt find anyone with this error
     
  10. SuperMD50

    SuperMD50

    Joined:
    Feb 5, 2020
    Posts:
    7
    actually nvm, thank you too everyone that helped me