- Home /
Assets Returning Null
I've just started using ScriptableObject to store sets of data as Assets. I plan to manipulate this data a lot, so this looked like a good way to store / load serialised data.
What I've got so far works for creating the assets. I can use the Assets menu in Unity to add them, and the files that appear in my Assets directory include the right fields / variables.
The problem is, when I hit the Play button in the Game window to test out my code, they don't seem to be usable in the scene. if I try to Debug.Log them, I always get "null" returned and the following message:
"The referenced script on this Behaviour is missing! UnityEngine.Resources:Load(String, Type)"
Afterwards, when I select the assets, I get the following message in the Inspector panel (instead of the fields / variables):
"The associated script can not be loaded. Please fix any compile errors and assign a valid script."
Is the "referenced script" message the compile error that this is referring to? When I create new Team assets, the Script field in the Inspector panel just says "None (Mono Script)", so I'm not sure why this only becomes a problem afterwards.
I assume I've missed something fundamental about creating / loading assets, and I'm not accessing them properly. If I select an asset by clicking on it in the Project panel, and then press the Play button on the Game window, the asset works and still has its data afterwards. I'm guessing that highlighting / selecting an asset is somehow doing what I've missed in my functions.
Here's the code I'm using:
public class Team extends ScriptableObject {
public var TeamName : String;
public var Rank : int;
}
public class TeamAsset {
@MenuItem ('Assets/Create/Team')
public static function create() {
var newTeam : Team;
var path : String = 'Assets/Resources/TeamData/Team.asset';
newTeam = ScriptableObject.CreateInstance(Team);
AssetDatabase.CreateAsset(newTeam, path);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
}
function loadTeam() {
var thisTeam : Team = Resources.Load('TeamData/Team', Team) as Team;
Debug.Log(thisTeam);
}
In this (simplified, tidied up) example of my code, the function loadTeam just logs null and gives the "referenced script" missing error. When I double click it, I'm taken to the Resources.Load line in my code.
I'm clearly missing something when it comes to saving / loading assets, but hours of Googling has turned up nothing workable (partly because I'm not entirely sure how to phrase the problem).
Any help very much appreciated. Thanks!