Search Unity

Resources.Load not working

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

Thread Status:
Not open for further replies.
  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:
    38,752
    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
  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:
    38,752
    If that is not called from the main thread, it probably won't work. Marshal it to the main thread.
     
    Nad_B likes this.
  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:
    38,752
    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
  10. mullenator

    mullenator

    Joined:
    Jun 30, 2022
    Posts:
    5
    Your advice/solution worked for me. Thanks Kurt-Dekker!
     
    Nad_B likes this.
  11. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    Please don't necropost. Please use the "Like" button to show appreciation.

    Thanks.

    Locking this thread.
     
    Bunny83 likes this.
Thread Status:
Not open for further replies.