Script help needed for a part of a Unity Learn Pathway (Junior Programmer, Mission 3)

I need help just understanding how to do this. I’ve been working on Unity Learn, Junior Programmer pathway, and I’m currently on Mission 3. I’m stuck on this tutorial, “Create a scene flow”, and on this part, “Add a new namespace”. Basically, this is the code I need help on:

if (UNITY_EDITOR)
{
using UnityEditor;
}

The tutorial said for it to be set up at the end of all the name spaces, and I set it up that way, and It wasn’t working. I’ve also tried other stuff, but it also didn’t work. Whenever I look at my project in Unity, it says there’s a error to the script, in that specific area. I just want to fix this so that I can playtest my project.

Thank you.

Other info

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;

if (UNITY_EDITOR)
{
    using UnityEditor;
}

// Sets the script to be executed later than all default scripts
// This is helpful for UI, since other things may need to be initialized before setting the UI
[DefaultExecutionOrder(1000)]
public class MenuUIHandler : MonoBehaviour
{
    public ColorPicker ColorPicker;

    public void NewColorSelected(Color color)
    {
        // add code here to handle when a color is selected
    }
   
    private void Start()
    {
        ColorPicker.Init();
        //this will call the NewColorSelected function when the color picker have a color button clicked.
        ColorPicker.onColorChanged += NewColorSelected;
    }

    public void StartNew()
    {
        SceneManager.LoadScene(1);
    }

    public void Exit()
    {
        if (UNITY_EDITOR)
        {
            EditorApplication.ExitPlaymode();
        }
       
        else
        {
            Application.Quit();
        }
      
    }
}

Look again closely at the code in step 2 and then compare it with what you typed in. :wink:

1 Like

I tried to replace the code with what it says on the lesson, but it came up with 4 errors. The lesson said for you to test it out in Unity, you’ll have to remove the conditional instructions, so I changed the #if UNITY_EDITOR & #endif into if statements. I’ve also just tried putting using UnityEditor, but it didn’t work.

You cannot use if/else (and many other statements) outside a method.

If you write the things EXACTLY as they are stated, they will work. I noticed you also used the UNITY_EDITOR in a method in an if statement. That won’t work either.

UNITY_EDITOR can only be used as a preprocessor statement:
#if UNITY_EDITOR
#endif

It will not work as such:
if (UNITY_EDITOR)
{
}

2 Likes

All right, it worked. Thank you, and sorry for the trouble. I’m new to scripting, and I read the lesson wrong. Thank you for helping me. :smile:

2 Likes

Just so you know, that lesson is confusing. It should have said to cut&paste those 3 lines exactly and not explained why, not until like chapter 12. Because that #if is a weird special deal, and even without it, the using is also a weird special deal, and we’re learned not to explain weird special deals in lesson 3 when there are still plenty of normal deals to learn.

3 Likes