1

In my Unity solution I have two projects, the main one and the editor one. I am creating an editor script and I would like to reference a class I have in the main project.

I've tried adding a namespace in the class I want to reference from the editor but Visual Studio don't detect it in the editor script.

I tried following this post: How can I refer to a project from another one in c#?

However when I try that I get the following error message: "A reference to 'Assembly-CSharp' could not be added. Adding this project as a reference would cause a circular dependency.

Many thanks!

Cris
  • 197
  • 1
  • 4
  • 16
  • 2
    In Unity, you can't. The script must be present in the Project for it to be used by other scripts or to be included in the build – Programmer May 23 '18 at 13:58
  • `A reference to 'Assembly-CSharp' could not be added. Adding this project as a reference would cause a circular dependency.` - This means that either the project you're trying to add includes the current project, or you're trying to add the project to itself. It means that if allowed, it will keep processing the projects infinitely as project A includes project B but project B includes project A, so add project A but add project B, which includes project A... – AStopher May 23 '18 at 13:59
  • @cybermonkey Or more likely, both projects use the same DLL name because Unity doesn't bother making it compatible. – Draco18s no longer trusts SE May 23 '18 at 14:44
  • @Programmer, thanks for letting me know - I didn't know that wasn't possible. However I've seen many assets (from the store) do what I am after, basically add a 'tool' component to some objects in the scene and then get a editor script to look for them and get the data they contain. Should I open a new question with the code I have so far? – Cris May 24 '18 at 01:09

2 Answers2

2

In Unity you can not reference a different project directly. You can however export classes, objects etc. into a package in Unity.

After exporting the package from your "Editor" project, you can then Import it in your "Main" project.

You can export a package with Assets > Export Package and select what you want to export:

enter image description here

AStopher
  • 4,207
  • 11
  • 50
  • 75
Immorality
  • 2,164
  • 2
  • 12
  • 24
  • Not sure what this has to do with accessing/referencing class from another project. Wouldn't it be easier to just copy the class file than exporting and importing the package? OP doesn't want to make a copy of that class. That's the reason behind the question. – PassetCronUs May 23 '18 at 18:00
  • 1
    Many thanks for your answer, however I think I phrased wrongly. I am not trying to reference game objects from project A in project B. I have a single project, with a single Solution, in this solution there are two projects (Visual studio projects): the main one and the editor one. I want to access a class of the main project in a class of the editor project. Apologies for the confusion and thanks for your time. – Cris May 24 '18 at 01:05
0

I came back to this after a while and found what I was looking for just in case it can be useful to anyone. What I wanted is called a Custom Inspector. Basically having a regular Monobehavior componenet and override its inpector with a UnityEditor script (many tools in the Asset Store use this).

Here it is greatly explained: https://unity3d.com/es/learn/tutorials/topics/interface-essentials/building-custom-inspector

Cris
  • 197
  • 1
  • 4
  • 16