- Home /
Reference list within a list
Hi,
I'm trying to create a very simple database one to many like list, in which I have:
A list of objects (cooker, toaster, box..)
A list of tools (bread, knife, cup...)
A list of the above objects, that can have many of the above tools, with other variables (this will be on how they interact, how long etc)
So I have something like the below as an example of what I'd like to achieve.
My main goal is to be able to populate these from the editor, so they can be changed at will, and saved against whatever object I put it on.
I can't figure our how to populate the UseableTool.obj or UseableTool.tool, with the other lists, doing it as shown below just creates further list objects.
Any thoughts/ideas?
Thanks, Geordie
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Data : MonoBehaviour {
[SerializeField] List<Object> objects = new List<Object>();
[SerializeField] List<Tool> tools = new List<Tool>();
[SerializeField] List<UseableTool> useableTools = new List<UseableTool>();
[System.Serializable]
public class Object {
public string name;
}
[System.Serializable]
public class Tool{
public string name;
public float cost;
}
[System.Serializable]
public class UseableTool{
public Object obj;
public Tool tool;
public float time;
public float effort;
}
}
Answer by spiceboy9994 · Apr 14, 2015 at 10:21 PM
Try this:
public class CustomData : MonoBehaviour {
public List<CustomObject> myObjects;
public List<Tool> myTool;
public List<UseableTool> useableTools;
}
[System.Serializable]
public class CustomObject {
public string name;
}
[System.Serializable]
public class Tool{
public string name;
public float cost;
}
[System.Serializable]
public class UseableTool{
public Object obj;
public Tool tool;
public float time;
public float effort;
}
And here's the explanation:
I put the extra classes declaration outside the class that inherit from monobehavior
I renamed the "Data" and "Object" classes to a more personal one. That may help you on when coding on the intellisense detection and avoid any possible naming conflict with native unity classes.
I exposed the List of objects as simple public variables within the MonoBehavior class. That should be enough to get the lists to the Editor.
Regards
thanks for the input, not quite what I was after, I was hoping to get a way to build the interactions between the tools and objects I set up in UseableTool and do all this within the Editor, so the UseableTool:obj and UseableTool:tool would be lists of what is entered in the myObjects and myTools lists. After playing around with this a bit and trying to do some Editor scripts, I decided to just use csv files... much easier :)
Your answer
