Search Unity

[SOLVED] Resources.Load not working properly with ScriptableObject?

Discussion in 'Scripting' started by eliahlohr, Jul 27, 2018.

  1. eliahlohr

    eliahlohr

    Joined:
    Mar 3, 2018
    Posts:
    2
    Hey to all of you out there,

    I have a problem with Resources.Load, or maybe I just fail to see the reason why it's not working. I am using ScriptableObjects to store the design of my games themes, so it's easy to create/modify them. Of course the theme the player picked last time, should be loaded the next time he plays, so when he enters the scene.

    Changing the theme works by calling a function (changeTheme) and giving it the new theme as an argument. This works, not just with buttons but also when entering the scene if I manually set the currentTheme variable in the inspector, wich is of course suppposed to be set by reading the PlayerPrefs for the name string and then loading the Asset with Resources.Load. However if I try to do this with Resources.Load, i get "NullReference Exception: Reference not set to an instance of an object." (The three lines noted in the error are marked in the script)

    Code (CSharp):
    1.     private void Start()
    2.     {
    3.         Player = GameObject.FindGameObjectWithTag("Player");
    4.         Platforms = GameObject.FindGameObjectsWithTag("Platform");
    5.         Barriers = GameObject.FindGameObjectsWithTag("Barrier");
    6.         Markers = GameObject.FindGameObjectsWithTag("MarkerPlatform");
    7.  
    8.         Debug.Log("Themes: Tagged objects found and assingned.");
    9.  
    10.  
    11.         currentTheme = Resources.Load<theme>("Assets/Themes/" + PlayerPrefs.GetString("selectedTheme", "standart"));
    12.         changeTheme(currentTheme);         //Line 1 in the error message
    13.  
    14.         Debug.Log("Themes: Applying saved Theme.");
    15.     }
    16.  
    17.  
    18.     public void changeTheme(theme newTheme)
    19.     {
    20.         currentTheme = newTheme;
    21.         updateScene();                   //Line 2 in the error message
    22.         PlayerPrefs.SetString("selectedTheme", currentTheme.ToString());
    23.     }
    24.  
    25.  
    26.     private void updateScene()
    27.     {
    28.         Player.GetComponent<Renderer>().material = currentTheme.playerMaterial; //Error 3
    29.  
    30. //there are more lines similar to that after this one but the script stops execuring right here.

    Is Resources.Load just not working properly or is it taking to long to load the ScriptableObject maybe? Any ideas are welcome, since I really don't have them on my own anymore.

    Sincerely, Eliah
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,291
    berber_hunter and eliahlohr like this.
  3. eliahlohr

    eliahlohr

    Joined:
    Mar 3, 2018
    Posts:
    2
    Ooooh okay! I didn't know that since I only read the page about the load function directly.

    Thank you very much Karl,

    Eliah
     
    karl_jones likes this.