Resources.LoadAssetAtPath problem

Hey Guys

I have a problem:

I wanna instantiate an ParticleSystem, which is in a folder in my project. I wanna load this ParticleSystem per path.

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour{
    private GameObject prefab;

    void Start(){
       prefab = Resources.LoadAssetAtPath("Assets/Artwork/ParticleSystem.prefab",
       typeof(GameObject)) as GameObject;

	   Instantiate(prefab,prefab.transform.position,prefab.transform.rotation);


    }
}

Ok now i can’t Instantiate because the “prefab” is = null. (I think it doesn’t find the object)

I couldn’t upload an image of the project folder so I describe:

Theres an folder Assets with an subfolder Artwork. In the artwork folder is an Empty object Particlesystem which contains the particlesystem.

Beside the Assets folder, there is an Scripts folder with this example script.

Thanks for your help!

3 Answers

3

Resources.LoadAssetAtPath is an Editor only function. It can’t be used in your actual game / application.

You can’t load an abitrary asset via a path in your game since Unity only includes those assets into a build which are referenced by a scene or script and those in a “Ressources” folder. See Resources.Load.

Read the pages carefully.

Did you read the documentation: Unity - Scripting API: Resources.LoadAssetAtPath

This function always return null in the standalone player or web player. This is useful for quickly accessing an asset for use in the editor only.

You need to use this method instead (Unity - Scripting API: Resources.Load), and put your assets under a resource hierarchy.

See http://unity3d.com/support/documentation/Manual/Loading%20Resources%20at%20Runtime.html

This is not a solution. What if you were to save changes and close unity before you did all the undos? Only solutions are to be marked as correct answers. If you don't find a solution in one of the answers someone has given, simply close the question and give a reason for closing.

Very strange since for me it is working.
Did you check the folder ?