Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

457
Visualizações
"The name "Asset Database" does not exist in the current context" error while building unity game

My teacher gave me a challenge in unity (c#) where I have to load prefabs into an array (as a GameObject[] function). The code I have works in the editor, but when I build the game, it gives me the error The name "Asset Database" does not exist in the curernt context. The code I have currently is:

using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEditor;
using System;

public class FindCards : MonoBehaviour
{
    public string directoryName;
    public List<GameObject> foundGameObjects;

    public GameObject[] ListOfCards()
    {
        // get the directory and the files in them
        DirectoryInfo dirInfo = new DirectoryInfo(Application.dataPath + directoryName);

        FileInfo[] fileInfo = dirInfo.GetFiles("*.prefab");

        // loop through the files
        foreach(FileInfo file in fileInfo)
        {
            // get the path to the directory again (with proper formatting)
            string fullPath = file.FullName.Replace(@"\","/");
            string assetPath = "Assets" + fullPath.Replace(Application.dataPath, "");
    
    
            GameObject _asset;
            // load the asset from the direcotry
            _asset = AssetDatabase.LoadAssetAtPath(assetPath, typeof(GameObject)) as GameObject;            

            // add it to the list
            foundGameObjects.Add(_asset);
            Debug.Log(_asset.name);
        }


        // return the list as an array
        return(foundGameObjects.ToArray());
    }

    public void SetCardProperties(string cardName, Card card)
    {
        // get the first string, and the int
        // from the card's name
        string[] sub = cardName.Split('_');

        // (if the name was Diamond_07,     // string name
        // there would be an int = 07 and   // name[0]
        // a string = Diamond)              // name[1]
        card.suit = sub[0];
        Int32.TryParse(sub[1], out card.number);
    }
}

The code that is calling this function looks like this (note that this is in a different script):

private void Start()
{
    canScore = true;
    gameIsGoing = true;

    // set up cards
    cards = findCards.ListOfCards();

    for (int i = 0; i < cards.Length; i++)
    {
        Card card = cards[i].GetComponent<Card>();

        findCards.SetCardProperties(cards[i].name, card);
    }

    // set up deck
    ResetDeck();
}

The directory here: Prefabs in the directory

Errors here: Errors unity gives me

I have been pulling my hair out, because there was nothing online I could find that would help me. My teacher gave this to me as challenge because he also did not know how to do it which is why I'm asking this question...

Thanks in advance!

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

AssetsDatabase it seems that works only in Editor. Check this thread for more info:

https://answers.unity.com/questions/1068055/assetdatabase.html

Edit:

`Resources.Load()´ search in the path: "Assets/Resources".

Look what documentation says:

If an asset can be found at path, it is returned with type T, otherwise returns null. If the file at path is of a type that cannot be converted to T, also returns null. The path is relative to any folder named Resources inside the Assets folder of your project. More than one Resources folder can be used. For example, a project may have Resources folders called Assets/Resources and @@AssetsAssetsResources@@. The path does not need to include Assets and Resources in the string, for example loading a GameObject at @@AssetspathShotgun.prefab@@ would only require Shotgun as the path. Also, if @@AssetspathMissiles/PlasmaGun.prefab exists it can be loaded using GunspathPlasmaGun@@ as the path string. If you have multiple Resources folders you cannot duplicate the use of an asset name.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda