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;
		
	}
}

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:

  1. I put the extra classes declaration outside the class that inherit from monobehavior
  2. 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.
  3. 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