- Home /
How can I get the name of a class into a script from the editor.
I'm making a game with multiple game modes and the best way I found to do what I want is to make an abstract base class for my TileManager and then inherit from it. This is all fine until I realized I needed to add a certain script, which I'd like to change in the editor, to a prefab that I Instantiate in some method. So here's some "code".
override public void createSlots()
{
public ScriptName ReferenceFromEditor;
.
.
.
GameObject slot = (GameObject)Instantiate(tileSlot, new Vector3(-camWidth + (i + 1) * distBetweenTiles - boxSizeX / 2, topLeft.y - curRow * boxSizeY * 2), Quaternion.identity);
slot.AddComponent<ReferenceFromEditor>();
}
So what I want to know is what to replace "ScriptName ReferenceFromEditor " with so I can get a reference to a script name from the editor.
The question might be better reworded as how can I use the editor with AddComponent<>().
Answer by tanoshimi · Oct 27, 2016 at 07:01 AM
Do you mean:
public Component ReferenceFromEditor;
Then you can assign any script in the editor to be attached with AddComponent.
Doesn't that require a reference to be in the game already? I don't have any instances of this component in the game yet.
I'm not quite sure I follow the problem: I thought you were trying to be able to instantiate a component in code (using AddComponent), where the type of that component was assigned by dragging a reference into the editor inspector? That's what my code snippet will do.
Yeah. I had tried that solution before, but you can't drag in a script from the asset menu like I'd need. There is no object in the game with this script on it yet. I need to create these objects then add a script based on that reference. Let me know if that makes sense.