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. Dismiss Notice

Problems with the name does not exist in the current context

Discussion in 'Scripting' started by ted_gress, Jun 21, 2018.

  1. ted_gress

    ted_gress

    Joined:
    May 31, 2013
    Posts:
    5
    I'm trying to write a profiler for Unity. All of the profiling code is in an editor folder and I have a script in the root folder. I'm trying to access a class in the profiling code in the editor folder but keep getting the above message.
    Code (CSharp):
    1.  
    2. namespace NJuiceBox
    3. {
    4.  
    5.     public class TestScript : MonoBehaviour
    6.     {
    7.         // Use this for initialization
    8.         void Start()
    9.         {
    10.             Debug.Log("Starting test script");
    11.             Profiler.ProfileBegin("TestScript");
    12.             Thread.Sleep(1000);
    13.             Profiler.ProfileEnd("TestScript");
    14.             Debug.Log("Ended profiler");
    15.         }
    16.         // Update is called once per frame
    17.         void Update()
    18.         {
    19.         }
    20.  
    21.     }

    Script code:

    Code (CSharp):
    1.  
    2. namespace NJuiceBox
    3. {
    4.  
    5.     public class TestScript : MonoBehaviour
    6.     {
    7.         // Use this for initialization
    8.         void Start()
    9.         {
    10.             Debug.Log("Starting test script");
    11.             Profiler.ProfileBegin("TestScript");
    12.             Thread.Sleep(1000);
    13.             Profiler.ProfileEnd("TestScript");
    14.             Debug.Log("Ended profiler");
    15.         }
    16.         // Update is called once per frame
    17.         void Update()
    18.         {
    19.         }
    20.  
    21.     }
     
    Last edited: Jun 22, 2018
  2. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,244
    Pasting non-formated code in General Discussion forum.
    Not even pasting the line index where the exception happened.
    Meta as f**k.
     
    Kurt-Dekker likes this.
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    And not telling us which name does not exist.

    Let me go get my crystal ball.
     
    Kurt-Dekker likes this.
  4. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,292
    I know Unity's profiler is not ideal, but like writing your own without any knowledge on how Unity's threads are managed is far worse idea.
     
    Kurt-Dekker likes this.
  5. ted_gress

    ted_gress

    Joined:
    May 31, 2013
    Posts:
    5
    Well thanks everyone for the help. This is my first post to the forum, so cut me a break.
    Code (CSharp):
    1. namespace NJuiceBox
    2. {
    3.  
    4.  
    5.     public class TestScript : MonoBehaviour
    6.     {
    7.  
    8.         // Use this for initialization
    9.         void Start()
    10.         {
    11.             Profiler profileObject = new Profiler();
    12.             Debug.Log("Starting test script");
    13.             Profiler.ProfileBegin("TestScript");
    14.             Thread.Sleep(1000);
    15.             Profiler.ProfileEnd("TestScript");
    16.             Debug.Log("Ended profiler");
    17.         }
    18.  
    19.         // Update is called once per frame
    20.         void Update()
    21.         {
    22.  
    23.         }
    24.  
    25.  
    26.     }
    27. }
    The static method Profiler.ProfileBegin and Profile.ProfileEnd are the two that show up with the error:

    Type or namespace Profiler could not be found.
    And there is no reason to criticize unless it is constructive. You and your crystal ball are pretty tough when you are behind a computer monitor.
     
  6. CubicCBridger

    CubicCBridger

    Joined:
    Apr 19, 2017
    Posts:
    44
    the code you submitted formatted is different to the unformatted code. In unformatted you are using in unityengine profiler. In formatted it probably can't tell if you are trying to use unityengine profiler or NJuiceBox profiler, don't really know. Please edit original post with code formatted (rather than posting a new message with a subset of orginal code, modified and formatted), and copy paste FULL error, then maybe we will be able to help you a little better.
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    No. If you want to participate here, you participate at the same standard as everyone else. Telling you where you messed up is the fastest way to get you up to standard.

    It looks like @CubicCBridger is on the right track. UnityEngine.Profiling.Profiler and NJuiceBox.Profiler are different classes. Its pretty easy to confuse the compiler so it goes to the wrong class. Try using the fully qualified type name. Or rename your class to something different.

    Alternatively you could just use the Unity profiler. It will do most things you ask of it.
     
    JuniorMaphosa likes this.
  8. ted_gress

    ted_gress

    Joined:
    May 31, 2013
    Posts:
    5
    The full error is "The name profiler does not exist in the current context"
    The fully qualified name gives the error: "The type or namespace name Profiler does not exist in the namespace 'NJuiceBox'.Are you missing an assembly reference. I think the script calling the profiler and the profiler are in different projects but I'm not sure.

    Also, I edited the first code posting.
     
  9. CubicCBridger

    CubicCBridger

    Joined:
    Apr 19, 2017
    Posts:
    44
    If you can't get the NJuiceBox Profiler to work (does it exist? Is there a class called Profiler in the NJuiceBox namespace?) as @Kiwasi said, unity has its own API for this stuff.

    https://docs.unity3d.com/ScriptReference/Profiling.CustomSampler.html
    https://docs.unity3d.com/ScriptReference/Profiling.Sampler.GetRecorder.html

    Using those two classes will allow you to measure time of code blocks without using third party libraries/ your own custom code.

    Create a sample, sample code with begin and end, get the recorder and inspect/call elapsed nanoseconds on recorder.
     
    Kiwasi likes this.
  10. ted_gress

    ted_gress

    Joined:
    May 31, 2013
    Posts:
    5
    Yes. There is a class called Profiler in the NJuiceBox namespace. I'm thinking it has to do with the Editor folder.. All the NJuiceBox code is in the Editor folder. The TestScript file however, is in the assets folder, Could that be the problem?
     
  11. CubicCBridger

    CubicCBridger

    Joined:
    Apr 19, 2017
    Posts:
    44
    yep that sounds like the issue, you can't have code outside an editor folder access code inside an editor folder (pretty sure). If you chuck your tests scripts inside editor folder they should be able to find the classes.
     
  12. ted_gress

    ted_gress

    Joined:
    May 31, 2013
    Posts:
    5
    Ok. That's what I thought. I want to have these scripts downloadable at some point from the asset store. I guess there is no way around this. I don't want the code for JuiceBox to be intermixed with the users code. Do you have a good link about the editor folder? I was under the impression it is used for plugins.
     
  13. CubicCBridger

    CubicCBridger

    Joined:
    Apr 19, 2017
    Posts:
    44