- Home /
"Type mismatch" in Inspector for GameObject type in asset file
Hi,
In the Custom Editor I am trying to create an Asset that keeps a list of GameObject references together with some other attributes and store this in an Asset.
What works (please refer to the code further down below for details):
The Asset file gets created fine.
List items are added to the list via the AddItem() method and are visible in the inspector.
New game objects are created in the hierarchy when object gets created via the AddItem() method.
What is not working:
When selecting the asset in the Project window I can see the list in the inspector but for every single Game Object field I can see "Type mismatch" message. I would expect the "Test" name displayed just as for Monobehaviour scripts with GameObject properties.
I cannot drag and drop game objects from the hierarchy to the inspector Game Object field. I would expect that I can drag and drop game objects from hierarchy just as for Monobehaviour scripts with GameObject properties.
When clicking the dot next to the Game Object field in the Inspector I cannot see game objects that I can select from the pop-up window.
What am I doing wrong ? Can someone help please.
Data structures:
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class ScriptableObjectData {
public GameObject gameObject;
public Texture2D texRex;
public int ExecutionOrder;
}
[System.Serializable]
public class ScriptableObjectDataManager : ScriptableObject {
public List<ScriptableObjectData> scriptableObjectDataList = new new List<ScriptableObjectData>();
}
Method that adds an item to the list:
public class ScriptableObjectUser : EditorWindow {
(...)
void AddItem() {
ScriptableObjectData data = new ScriptableObjectData();
data.ExecutionOrder = ++counter;
data.gameObject = new GameObject("Test");
dataManager.scriptableObjectDataList.Add(data);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
(...)
}
Hello ? This is simple stuff to reproduce.
Can anyone help me please ?
$$anonymous$$any thanks.
Answer by Bunny83 · Sep 17, 2017 at 10:39 AM
Obviously an asset stored in the project can't reference anything that is located in a scene. Everything that is located in a scene is only available when the scene is loaded / active. Assets are presistent across the whole project and they can only reference other assets in the project.
The reverse is possible. Objects in the scene can reference other objects in the same scene or any asset stored in the project as assets are always available unlike objects in a scene.
If you want some sort of manager that holds references to objects in the scene you have to create it as MonoBehaviour an attach it to an object in the same scene.
... and that is why I could only see "Assets" tab and not any other Game Objects after pressing circle next to Game Objects.
But when I add Animation type property to my data type I could see the list of available animations and choose one of them to be stored in my Asset.
Got it ! $$anonymous$$any thanks !
Answer by meebou · May 08, 2019 at 12:06 PM
came here to the same problem.everybody says DONT USE SINGLETON...use SCRIPTABLE OBJECTS....and now nothing works like it has worked before :-( so hello singleton again ;)
Your answer
Follow this Question
Related Questions
Importing and using custom assets 0 Answers
"Unbroken Reference" problem when using a custom Editor to Save/Load a ScriptableObject Asset 0 Answers
Getting Reference to Asset without calling LoadAsset 0 Answers
Display List of Objects in Editor Window 2 Answers
Create script from template, then create .asset file from new script 1 Answer