Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Whenever I move a script to a different folder, other scripts can't find it

Discussion in 'Scripting' started by JMVRyt, Jul 30, 2023.

  1. JMVRyt

    JMVRyt

    Joined:
    Jul 4, 2019
    Posts:
    6
    I have a basic scene with nothing in it. In the Project hierarchy, I only have 2 folders: Scripts and Scenes. Inside Scripts, I have Timer.cs, UIHandler.cs, and a Tests directory. Inside the Tests directory, I have Tests.asmdef and TimerTest.cs.

    Here is the tree-view of the Assets folder:
    Code (Text):
    1. Assets
    2. │   Scenes.meta
    3. │   Scripts.meta
    4. ├───Scenes
    5. │       SampleScene.unity
    6. │       SampleScene.unity.meta
    7. └───Scripts
    8.     │   Tests.meta
    9.     │   Timer.cs
    10.     │   Timer.cs.meta
    11.     │   UIHandler.cs
    12.     │   UIHandler.cs.meta
    13.     │
    14.     └───Tests
    15.             Tests.asmdef
    16.             Tests.asmdef.meta
    17.             TimerTest.cs
    18.             TimerTest.cs.meta
    Tests.asmdef and TimerTest.cs are for the unit testing I have made for the Timer.cs file, which is a custom Timer script, which does not inherit from MonoBehaviour.
    UIHandler.cs is for handling changing the UI whenever the Timer reaches the MaxTime.

    Unfortunately, this is where the errors come into play. In the current state, TimerTest.cs can't find my Timer.cs script. Visual Studio 2022 says "The type or namespace name 'Timer' could not be found (are you missing a using directive or an assembly reference?)". Unity says the same thing.

    So, I thought to move the Timer.cs script up into the Tests/ directory. That fixed the errors for that script, but then UIHandler.cs starts throwing the same error. Then, I tried moving Timer.cs down into the base Assets directory, which caused UIHandler.cs to work again, but TimerTest.cs threw errors again. So it seems like the script only has errors because of the unit testing folder???

    I tried giving the Timer a namespace, so that hopefully that just so happens to allow it to find my script. Nope. Instead, it can't find the namespace as well.

    I am confused. What can I do to fix this error from occurring? Do I have to get rid of the unit testing? Can I somehow get the unit tests to find the script, as well as other scripts being able to use the Timer? Or is the only solution just giving up and scrapping this entire thing?
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,008
    Note the diagram in the assembly definition doc page: https://docs.unity3d.com/Manual/ScriptCompilationAssemblyDefinitionFiles.html
    The primary Assembly-CSharp assembly references all other assemblies, including your own, but as it's a one-way relationship your assemblies can't reference anything in the main assembly.

    So generally if you're using assembly definitions, all your code needs to be organised into assemblies. So the solution is to just make an assembly for your main scripts, and have your test assembly reference the main assembly.
     
    CodeSmile likes this.
  3. JMVRyt

    JMVRyt

    Joined:
    Jul 4, 2019
    Posts:
    6
    Well, that did it. I didn't realize it was as simple as "make a folder called 'Timer', put Timer.cs into there, add an assembly definition asset into there named 'Timer', and make TimerTest.asmdef reference that".

    Now I'm wondering, did the manual for Unit Testing mention needing to do that and I overlooked it, or did they fail to mention that errors may occur when doing that? Because the manual makes it all seem like it's just plug-and-play and everything will just work out, when in reality it doesn't and needs things like that. Maybe they assumed that the dev won't make their own class and unit test that? Idk, just seems a little overlooked.
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,008
    Probably assumed if someone was at the stage of doing Unit tests they would understand how to use assembly definition assets.
     
  5. JMVRyt

    JMVRyt

    Joined:
    Jul 4, 2019
    Posts:
    6
    They probably didn't expect someone like me who's smart enough to do more advanced stuff, but with patchy knowledge. Because that's just the thing, I got interested in unit testing because of two YouTubers that I watched talking about its importance, but I guess I didn't realize just how more advanced unit testing is compared to everything else I've had to do regarding Unity.

    But my patchy knowledge may be the reason why I haven't had problems with Unity. I've done stuff with SceneManager.LoadSceneAsync, shaders, and other more technical/difficult things, but I haven't had problems. Then, when it comes to assembly definition assets, I get confused because I've never had to use them before and haven't seen any similar issues on Google.
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,008
    I mean did you read the docs before making this post?
     
  7. JMVRyt

    JMVRyt

    Joined:
    Jul 4, 2019
    Posts:
    6
    For assembly definition assets specifically, no. For unit testing using NUnit, yes. Specifically, what happened was I searched up "Unity Unit Testing" on Google and got a link to https://docs.unity3d.com/Packages/com.unity.test-framework@1.3/manual/index.html, so I followed that. I just used the default Test script that clicking the button supplied, and everything seemed to work fine, until I created a different script, which caused the problems at the top of the thread.

    When I searched for similar issues where that error kept happening, everything just said "well it turns out I accidentally deleted the .cs.meta file, adding it fixed it nvm" or similar, which was definitely not my problem
     
    Last edited: Jul 31, 2023