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 How to get the location or path of above folder of the current script?

Discussion in 'Scripting' started by TheUninvited, May 7, 2020.

  1. TheUninvited

    TheUninvited

    Joined:
    Jun 22, 2018
    Posts:
    31
    So my structure is like this
    -Test
    --Editor (in here i have a script lets say myscript.cs)

    And i want from myscript.cs to grab the location(path) of the Test how would i do that?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    Your script doesn't know where its own script file is, and if you feel like it needs to, I'd suggest that you should step back and rethink what you're trying to accomplish, because it sounds like whatever it is is probably a bad idea. Why do you need this?

    If you're just looking for the Assets folder in general, Application.dataPath when called in the Editor will do that.
     
    Kurt-Dekker likes this.
  3. TheUninvited

    TheUninvited

    Joined:
    Jun 22, 2018
    Posts:
    31
    I found this So when i execute this it gives me this path
    Test/Editor/myscript.cs
    but what i want is the path for Test folder

    Any idea how to do this please?

    Code (CSharp):
    1.  string GetCurrentFileName([System.Runtime.CompilerServices.CallerFilePath] string fileName = null)
    2.     {
    3.         return fileName;
    4.     }
     
    forestrf likes this.
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    It's a fairly simple string parsing problem at that point, but the fact that you're doing something so bizarre that I've never seen nor heard of anyone doing it in 14 years of coding professionally, suggests to me that this is almost certainly the wrong way to go about doing whatever it is that you're doing.

    I know it may be frustrating that I'm not giving you the answer, but I honestly feel that that would be irresponsible at this point without knowing what your actual end goal with this is. If my instinct is incorrect and this is the correct way to go about doing what you want to do, I'll happily provide the final steps.

    So: what are you trying to do here?
     
    Kurt-Dekker likes this.
  5. Noblauch

    Noblauch

    Joined:
    May 23, 2017
    Posts:
    256
    @StarManta I'm currently searching (which is not hard at 7.5k posts (gz for that btw)) and reading your answers just for fun. They are hilarious and your signature with the link to a broken page makes it even better :D
    Sorry, I really enjoy your dry way of putting something while still trying to help as best as you can xD Indeed you seem to have some veteran experience :p

    @TheUninvited You need this:
    Code (CSharp):
    1. using System.IO;
    2. string wrongPath = "Test/Editor/myscript.cs";
    3. string twoUp = "../../";
    4. string youWantThis = Path.GetFullPath(Path.Combine(wrongPath, twoUp));
    Have fun regretting it at some point xP
     
    Sarai and PraetorBlue like this.
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    The gitbook link works for me...?
     
  7. Noblauch

    Noblauch

    Joined:
    May 23, 2017
    Posts:
    256
    "Can't reach server" Tested in Safari and Firefox from location Germany. Not sure whats wrong there :S
    Edit: Can't even reach gitbooks.io Error 502 – Bad Gateway
     
  8. TheUninvited

    TheUninvited

    Joined:
    Jun 22, 2018
    Posts:
    31
    Nope i knew this but the test folder is not static that's why i wanted to get the above folder.
     
  9. matkoniecz

    matkoniecz

    Joined:
    Feb 23, 2020
    Posts:
    170
    It is working for me
     
  10. cdr9042

    cdr9042

    Joined:
    Apr 22, 2018
    Posts:
    165
    This answer by @Leslie-Young can do that, assuming the script's name is unique inside the project https://answers.unity.com/questions/306751/get-script-path.html

    Code (CSharp):
    1.  string[] res = System.IO.Directory.GetFiles(Application.dataPath, "YourEitorScript.cs", SearchOption.AllDirectories);
    2. if (res.Length == 0)
    3. {
    4.      Debug.LogError("error message ....");
    5.      return null;
    6. }
    7. string path = res[0].Replace("YourEitorScript.cs", "").Replace("\\", "/");
    8. Debug.Log(path);
    This answer from Baste could also work

     
    Last edited: Jan 30, 2021
    Sarai likes this.
  11. Mutimir

    Mutimir

    Joined:
    Dec 6, 2018
    Posts:
    36
    Thank you @TheUninvited for asking such an unconventional question and providing an answer for it. @StarManta I understand your reluctance here but quite frankly I am a veteran as well and found use for such a code snippet.