Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Guedez · Jan 20, 2013 at 05:16 PM · loadresourcesnull

Resources.load returns null

I've searched many other similar questions but none of the solutions to those helped me...

The problem is when i use Resources.load("myResource"); it always returns null.

The code(C#):

 public class Fuser : MonoBehaviour {
 
     public Mesh a;
     public Mesh b;
     public GameObject t;
     void Start () {
         a = Resources.Load("floor", typeof(Mesh)) as Mesh;
         b = Resources.Load("floor") as Mesh;
         //a.CombineMeshes(b);
         
         //MeshFilter[] meshFilters = GetComponentsInChildren(MeshFilter);
         CombineInstance[] combine = new CombineInstance[2];
         //for ( i = 0; i < 2; i++){
             combine[0].mesh = a;
             combine[0].transform = new Matrix4x4();
             combine[0].mesh = b;
             Matrix4x4 m = new Matrix4x4();
             m.SetTRS(new Vector3(0,2,0), Quaternion.identity, Vector3.up);
             combine[0].transform = m;//new Vector3(0,2,0);//a.transform.localToWorldMatrix;
             //meshFilters[i].gameObject.active = false;
         //}
         t.GetComponent<MeshFilter>().mesh = new Mesh();
         t.GetComponent<MeshFilter>().mesh.CombineMeshes(combine);
         //transform.gameObject.active = true;
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 }

And the project structure:

alt text

I have tried many different ways and names but none seems to work... I don't know what i am doing wrong...

sem título.png (13.8 kB)
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Julien-Lynge · Jan 20, 2013 at 07:34 PM 0
Share

Sorry, but which line in your code is returning a null reference exception? Just after you load 'floor', have you tried checking whether a and b are null? (e.g. if(a == null) Debug.Log("A is null");

avatar image bernardfrancois · Jan 20, 2013 at 10:02 PM 0
Share

I suspect that the file you're loading may not be a mesh - perhaps it's rather something containing a mesh.

Do you also get a null return value if you simply try to store it in a variable of type Object? If it's an object of type GameObject, you may have to call .renderer.mesh on it in order to obtain the mesh.

avatar image Guedez · Jan 21, 2013 at 01:01 AM 0
Share

Yes, it was the whole 'as $$anonymous$$esh' and it was loading a GameObject... Funny how it returns null ins$$anonymous$$d of a error... I would never have guessed that one, thanks.

How do i mark as this being the answer?

avatar image Julien-Lynge · Jan 21, 2013 at 03:03 AM 0
Share

BTW: The fact that it returns 'null' is one of the effects of casting with 'as' (and the reason to use it) - it's often desirable to return a null rather than throw an error. Alternative ways of casting, for instance using (GameObject), will throw an error.

Unfortunately, you can't mark a comment as being an answer, but if Bernard will convert his comment to an answer you can mark it.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by numberkruncher · Jan 21, 2013 at 01:08 AM

When meshes are imported into Unity, a game object is added for the root object and then one for each sub-mesh. In your case you are attempting to access the container object as opposed to the mesh.

You can use the following method to access the mesh(es) in your asset:

 using System.Linq;

 ...

 GameObject floorModel = Resources.Load(
     "floor", typeof(GameObject)
 ) as GameObject;

 MeshFilter[] meshFilters =
     (MeshFilter[])floorModel.GetComponentsInChildren(
         typeof(MeshFilter)
     );

 Mesh[] meshes = (Mesh[])meshFilters.Select(
     filter => filter.sharedMesh
 );
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Resources.load returns null instead of AudioClip 1 Answer

How do I make sure a resource is loaded or is instantiated? 2 Answers

load resources material 0 Answers

Accessing 4.6 GUI build-in sprites in C# 0 Answers

Resources.Load wont read text file 6 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges