- Home /
Selecting Structs from dropdown menu in inspector.
I need to be able to select these struct objects from a dropdown menu in inspector in another script. If anyone has any idea on how to go about that it would be much appreciated.
Edit: I need to be able to select one of the objects to be used in a separate script.
So Idealy I should be able to select from the 'fault' dropdown menu one of those objects in the CircularHouse class. Selecting the fault from the dropdown will tell this class what fault from the CircularHouse class to use as the 'this fault' variable.
public class BathRoomFaultController : MonoBehaviour
{
public enum Houses{circular,oakField,mainStreet};
public Houses houses;
public FaultObject thisFault; //This will be the fault that this instance of the sript will use.
Canvas faultOptionCanvas; //The canvas for the fault.
TextMeshProUGUI faultInfoText; //The information on the fault.
TextMeshProUGUI Option01; //The text displated on the option1 button
TextMeshProUGUI Option02; //The tect displayed on the option2 button
TextMeshProUGUI Option03; //The text displayed on the option3 button
GameObject trigger; //The trigger point of the fault.
Transform cameraPos; //Position of the main camera.
private int UserSelection; //The option the user has selected.
void Start()
{
//--------------------Referances--------------------------------------------------------------
cameraPos = GameObject.FindGameObjectWithTag("MainCamera").transform;
trigger = GetComponentInChildren<SphereCollider>().gameObject;
faultOptionCanvas = GameObject.FindGameObjectWithTag("FaultCanvas").GetComponent<Canvas>();
faultInfoText = GameObject.FindGameObjectWithTag("FaultText").GetComponentInChildren<TextMeshProUGUI>();
Option01 = GameObject.FindGameObjectWithTag("Option01").GetComponentInChildren<TextMeshProUGUI>();
Option02 = GameObject.FindGameObjectWithTag("Option02").GetComponentInChildren<TextMeshProUGUI>();
Option03 = GameObject.FindGameObjectWithTag("Option03").GetComponentInChildren<TextMeshProUGUI>();
//--------------------------------------------------------------------------------------------
//Assign the fault.
thisFault = GetFault();
//Fill in the fault button info.
Option01.text = thisFault.faultOptions[0];
Option02.text = thisFault.faultOptions[1];
Option03.text = thisFault.faultOptions[2];
//Disable the fault canvas.
faultOptionCanvas.gameObject.SetActive(false);
}
What do you mean by "select"? if you need to be able quickly assign different configurations which may contain in these structs, you can create scriptable object wrapper for your struct, this will allow you to create serialised assets that you can then reference from whatever script you want in inspector.
Hi, I'm not too sure what you mean by that. Is there any documentation you could point me to that would help me out?
it is super simple. create a wrapper for your struct:
[CreateAsset$$anonymous$$enu(menuName="Data/Fault")]
public class FaultAsset : ScriptableObject {
public FaultObject fault;
}
then you can create as much Fault Assets as you want, all with different values and then reference them in any script you like.
Answer by misher · Sep 11, 2018 at 11:35 AM
Switching to Scriptable Objects.
Define your data structure
using UnityEngine;
[CreateAssetMenu(menuName ="Data/Fault")]
public class FaultAsset : ScriptableObject
{
public string[] faultOptions; //The options of the fault.
public string[] faultInfo; //The info for the fault.
public int[] faultAnswer; //The answer for the fault.
}
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(menuName ="Data/Fault Collection")]
public class FaultsCollectionAsset : ScriptableObject {
List<FaultAsset> faults;
}
using UnityEngine;
[CreateAssetMenu(menuName ="Data/Circular House")]
public class CircularHouseAsset : ScriptableObject {
public FaultAsset completeDwell;
public FaultAsset bathroom;
public FaultAsset mRoom;
public FaultAsset external;
}
Migration:
using UnityEngine;
public class Test : MonoBehaviour
{
[SerializeField] CircularHouse circularHouse;
[ContextMenu("SaveCircularHousAsAsset()")] // this allows you to invoke this method from inspector
/// <summary>
/// This is temporary migration method,
/// remove after migration is completed
/// </summary>
void SaveCircularHousAsAsset()
{
#if UNITY_EDITOR
Debug.Log("Migrate Circular House started...");
if (UnityEditor.AssetDatabase.IsValidFolder("Assets/Data") == false)
UnityEditor.AssetDatabase.CreateFolder("Assets", "Data");
CircularHouseAsset circularHouseAsset = ScriptableObject.CreateInstance<CircularHouseAsset>();
circularHouseAsset.name = "Circular House Asset 1";
var bathroom = ScriptableObject.CreateInstance<FaultAsset>();
bathroom.faultAnswer = circularHouse.bathroom.faultAnswer;
bathroom.faultInfo = circularHouse.bathroom.faultInfo;
bathroom.faultOptions = circularHouse.bathroom.faultOptions;
UnityEditor.AssetDatabase.CreateAsset(bathroom, "Assets/Data/bathroom.asset");
circularHouseAsset.bathroom = bathroom;
// cintinue for all
//........
UnityEditor.AssetDatabase.CreateAsset(circularHouseAsset, "Assets/Data/" + circularHouseAsset.name + ".asset");
Debug.Log("Migrate Circular House finished!");
#endif
}
}
This is the way I ended up doing it.
[CreateAsset$$anonymous$$enu(menuName = "Data/Fault")]
public class FaultAsset : ScriptableObject
{
public string faultName; //The name of the fault.
public string[] faultOptions; //The options of the fault.
public string[] faultInfo; //The info for the fault.
public int[] faultAnswer; //The answer for the fault.
}
public class DataSet : $$anonymous$$onoBehaviour {
public FaultAsset Create$$anonymous$$yAssets(string _house, string _name, string[] _faultOptions, string[] _faultInfo, int[] _faultAnswer)
{
FaultAsset asset = ScriptableObject.CreateInstance<FaultAsset>();
asset.faultName = _name;
asset.faultOptions = _faultOptions;
asset.faultInfo = _faultInfo;
asset.faultAnswer = _faultAnswer;
if (_house.Equals("Oakfield")){
AssetDatabase.CreateAsset(asset, "Assets/Data/Oakfield/" + _name + ".asset");
}
if (_house.Equals("Circular"))
{
AssetDatabase.CreateAsset(asset, "Assets/Data/Circular/" + _name + ".asset");
}
AssetDatabase.SaveAssets();
return asset;
}
private void Start()
{
#region Oakfield Houses
//Oakfield
Create$$anonymous$$yAssets(
"Oakfield",
"Complete Dwell One",
new string[] { "V896001", "V895001", "V896003" },
new string[] { "TEST:UN...of Rates).\n\n" },
new int[] { 1 }
);
Create$$anonymous$$yAssets(
"Oakfield",
"Complete Dwell Two",
new string[] { "460010", "460009", "V460003" },
new string[] { "DWELL...60033 or 460035).\n" },
new int[] { 3 }
);
Create$$anonymous$$yAssets(...//and so on
I'm having a real weird Issue though, It seems like a bug. The scriptable objects get created fine and when the scene is loaded the all load in fine. But when I exit the scene only the scriptable objects that I have highlighted stay normal the others look like this...
This seems like a bug in unity but i'm also getting an error log saying "No script asset for FaultAsset. Check that the definition is in the file of the same name."
I mean the scriptable objects are there but then they just keep going away. I thought because they were objects surley they would be safe from anything once they are actually created.
Never $$anonymous$$d put the fault asset in a seperate script file that sorted it.
First, why are you doing it in Start (and supposedly during Runtime). You can create your assets in any folder you want in your project by just clicking with right-mouse-button->Create->[you should see a new menu called "Data/Fault"]. The example I provided is just for migration purposes, say you don't want to retype all data you have somewhere in your serialized structs and you can transfer this data into new scriptable objects assets using temporary created methods that you can invoke from inspector (without running the game).