- Home /
how to use Scriptable Objects outside editor
Hello, I have recently started using Scriptable Objects its worked fine while in editor
But doesn't work when I build the client I have researched a bit and found that it something has to do with Resource folder but still entirely sure how to do it?
It shouldn't be necessary to involve the Resources folder, althouth there are, of course, situations where it comes in handy. But if you have problems using your ScriptableObjects in a build, it's not because you didn't use the Resources folder. Ins$$anonymous$$d, how are your SOs linked into your scene?
By referring the SO through the editor
public invclassSO inv;
And then dragging it on the inspector
That should absolutely work. I'd recommend building a $$anonymous$$imal example of your problem. A simple SO that holds one string and a $$anonymous$$onoBehaviour that assigns this string to a UI text component. If this works, try to find out where the difference to your non-working code is. If it doesn't, post your $$anonymous$$imal code here.
Could you be more precise on what exactly doesn't work, but works in editor?
This is the screenshot of the inspector
Code
public $$anonymous$$apsData $$anonymous$$aps;
Error Line
string name = $$anonymous$$aps.$$anonymous$$apDataList[0].name;
This work perfectly when run inside editor but when in Build it throws error
NullReferenceException: Object reference not set to an instance of an object at G:\Unity\Projects\Demov54\Assets\Scripts\$$anonymous$$aps\$$anonymous$$ap$$anonymous$$anager.cs:105
Is it possible that your ScriptableObject is - directly or indirectly - in a folder called "Editor"? This would cause the SO to be ignored in the build.
Answer by NoseKills · Jun 22, 2017 at 06:35 AM
I think @Bunny83 lead us to the right track. According to your screenshot you have a scene file dragged into that MapDataObj
field. Scene files are editor only assets that don't get packed into builds as such. The scene fields will become null in builds
With an editor script you could probably do so that you keep the scene object reference in the editor but have the editor also track changes in it & store the name of it as a string for use in builds (or just use a manually typed string but then they'll go offsync if you rename scenes)
If this is the case, this also works as a reminder that when you post questions about errors you get, it would make everything much faster if you posted the error (which contains the line number) and the code in such format that the line numbers match with the error. Reading 150 lines of code is a lot to ask when you could do with reading just 1 :)
Problem was that exactly cause I was using Scene Data to be stored on the SO
Answer by lorddanger · Jun 21, 2017 at 12:52 PM
Here is the code
[RequireComponent(typeof(NPCSpwaner))]
[RequireComponent(typeof(UnityClient))]
public class MapManager : MonoBehaviour
{
[SerializeField]
public int MapID;
[SerializeField]
[HideInInspector]
private Collider TownArea;
[SerializeField]
private Collider HuntingArea;
[SerializeField]
private UnityClient client;
public Collider GetTownArea
{
get { return TownArea; }
protected set { TownArea = value; }
}
public Collider GetHuntingArea
{
get { return HuntingArea; }
set { HuntingArea = value; }
}
// public Collider HuntingArea { get; protected set; }
#region Varibles
public MapsData Maps;
[HideInInspector]
public MapHelper _mapHelper;
NPCSpwaner ns;
#endregion
private void Awake()
{
_mapHelper = new MapHelper();
client = GetComponent<UnityClient>();
ns = GetComponent<NPCSpwaner>();
SetMapData();
}
void SetMapData()
{
Map _map = GetMap();
MapID = _map.id;
ns.MixedMap = _map.MixedMap;
ns.isTownOnly = _map.isTownOnly;
ns.isHuntingOnly = _map.isHuntingOnly;
ns.isOnlyInsideHuntingArea = _map.isOnlyInsideHuntingArea;
GetAreas();
if (GetTownArea != null)
{
ns.TownArea = GetTownArea;
}
if (GetHuntingArea != null)
{
ns.HuntingArea = GetHuntingArea;
}
}
void GetAreas()
{
GameObject areaObj = GameObject.FindGameObjectWithTag("Area");
AreaInfo AreaInfo = areaObj.GetComponent<AreaInfo>();
if (AreaInfo.TownArea)
{
GetTownArea = AreaInfo.TownArea;
}
if (AreaInfo.HuntArea)
{
GetHuntingArea = AreaInfo.HuntArea;
}
}
Map GetMap()
{
Map mp = null;
string name = SceneManager.GetActiveScene().name;
bool checkmap = Maps.MapDataList[0]; // Edited
int id = -1;
if (checkmap)
{
mp = Maps.MapDataList[0]; // Edited
id = mp.id;
}
return mp;
}
void SetMaptypes(GameObject ta, GameObject ha)
{
ns.MixedMap = false;
ns.isTownOnly = false;
ns.isHuntingOnly = false;
if (ta != null && ha != null)
{
ns.MixedMap = true;
}
else if (ha == null)
{
ns.isTownOnly = true;
}
else if (ta == null)
{
ns.isHuntingOnly = true;
}
}
public void MapDataFromServer(object sender, MessageReceivedEventArgs e)
{
_mapHelper.ReceivedMapData(sender, e);
ns.Spwaner(_mapHelper.Map);
}
public void CreatTheWriterForSpwanList(List<int> FailedSpwanUids)
{
DarkRiftWriter writer = new DarkRiftWriter();
writer.Write(MapID);
if (FailedSpwanUids.Count > 0)
{
foreach (int u in FailedSpwanUids)
{
writer.Write(u);
}
SendDataToServer(Tags.MapManager, Tags.MapManagerSubjects.FailedNPCSpwan, writer);
}
}
void SendDataToServer(byte tags, ushort Subject, DarkRiftWriter writer)
{
//Send Failed Position to srever
Debug.Log("Sent The failed position to the server");
TagSubjectMessage message = new TagSubjectMessage(tags, Subject, writer);
client.SendMessage(message, SendMode.Unreliable);
}
}
A few points:
First of all you posted an answer to your question but you don't answer anything in your post. If you want to improve your question, you should edit the question.
Second the line you mentioned in your comment above doesn't seem to exist in your actual code. Either you have changed your code or something else is not right.
Finally the line which might be closest to the offending line (line 85 in your code here) is a bit strange.
This is the line 85 of your code:
bool checkmap = $$anonymous$$aps.$$anonymous$$apDataList.Exists(x => x.$$anonymous$$apDataObj.name == name);
What exactly is stored / referenced in that $$anonymous$$apDataObj variable of your $$anonymous$$ap object? Why do you use the variable type UntiyEngine.Object ? Are you sure that every $$anonymous$$ap object in your List has a valid object assigned to it's $$anonymous$$apDataObj variable?
First of all you posted an answer to your question but you don't answer anything in your post. If you want to improve your question, you should edit the question. Ans :: At the time of posting the code I was outside and did that with my Phone and I was not able edit my initial question
Second the line you mentioned in your comment above doesn't seem to exist in your actual code. Either you have changed your code or something else is not right. Ans :: Yes that code was from the main project not from the test script which I was using to for fixing it
Finally the line which might be closest to the offending line (line 85 in your code here) is a bit strange.
And the final the problem is mainly then after build the $$anonymous$$aps which holds the SO become null
You should be able to edit questions on your phone too by pressing the gear button -> edit , or post a comment ins$$anonymous$$d. Under answers and questions there's a Add comment button. When commenting on a comment on mobile you have to tap the comment to bring up the Reply -option
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Is it possible to change builds structures? 2 Answers
Scriptable Objects not appearing on Build 0 Answers
How To Refresh Resources Folder in Build? 1 Answer
Building for Mod Compatability 1 Answer