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.

Resources.Load not working

Discussion in 'Scripting' started by kirisakichioge, Jul 7, 2021.

  1. kirisakichioge

    kirisakichioge

    Joined:
    Dec 25, 2019
    Posts:
    6
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Is the file you want to load inside your "Resources" folder?
     
  3. kirisakichioge

    kirisakichioge

    Joined:
    Dec 25, 2019
    Posts:
    6
    when i write the same code in start function ,is working ,What bothers me is why the following code is not executed
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    31,182
    For one thing, never use
    Resources.Load() as T


    Always use the
    Resources.Load<T>()
    form, in this case with a T of GameObject.

    The reason: using
    Resources.Load("Foo") as GameObject
    might load a material or something else named "Foo" that is not a GameObject. Then when you cast-as to GameObject it will come out mysteriously null and cause errors.

    More insidiously, if you have Foo.png and Foo.prefab, it is a 50/50 chance of what you'll get, and it may vary from install to install. Don't tempt fate.

    If you use
    Resources.Load<GameObject>( "Foo")
    (or also
    Resources.Load<Texture2D>( "Foo")
    ) you will get the correct object every time, assuming you meet the directory naming requirements (see docs).

    https://forum.unity.com/threads/sho...anging-texture-by-script.872356/#post-8403963

    For two things, make a fresh script, test the load function without all the network and callback stuff. Get that working first. Start with the Resources.Load<T>() script documentation to ensure you are meeting 100% of the requirements. There are several very specific requirements.
     
    Last edited: Aug 31, 2022
    Luis2095 likes this.
  5. kirisakichioge

    kirisakichioge

    Joined:
    Dec 25, 2019
    Posts:
    6
    upload_2021-7-8_1-43-3.png

    upload_2021-7-8_1-42-35.png
    thanks,the code in update function is executed,but openform function did not work correctly,maybe is async problem?my english is not well,i don't know if i made it clear (T⌓T)
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    31,182
    If that is not called from the main thread, it probably won't work. Marshal it to the main thread.
     
  7. kirisakichioge

    kirisakichioge

    Joined:
    Dec 25, 2019
    Posts:
    6
    thanks,I'll give it a try
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    31,182
    I don't really spend much time looking at code in a PNG file... that's why if you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    Looking above, the traditional way you handle things line "OnConnect()" is that you do almost nothing. You just set a boolean to true and store down any data you might get back, nothing else.

    Then in your Update() loop it goes "Hey, I see the boolean is true!" and proceeds.
     
  9. kirisakichioge

    kirisakichioge

    Joined:
    Dec 25, 2019
    Posts:
    6