Search Unity

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

Discussion in 'Scripting' started by yackbruh, Jan 28, 2024.

  1. yackbruh

    yackbruh

    Joined:
    Sep 13, 2023
    Posts:
    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


    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine.SceneManagement;
    5.  
    6. if (UNITY_EDITOR)
    7. {
    8.     using UnityEditor;
    9. }
    10.  
    11. // Sets the script to be executed later than all default scripts
    12. // This is helpful for UI, since other things may need to be initialized before setting the UI
    13. [DefaultExecutionOrder(1000)]
    14. public class MenuUIHandler : MonoBehaviour
    15. {
    16.     public ColorPicker ColorPicker;
    17.  
    18.     public void NewColorSelected(Color color)
    19.     {
    20.         // add code here to handle when a color is selected
    21.     }
    22.    
    23.     private void Start()
    24.     {
    25.         ColorPicker.Init();
    26.         //this will call the NewColorSelected function when the color picker have a color button clicked.
    27.         ColorPicker.onColorChanged += NewColorSelected;
    28.     }
    29.  
    30.     public void StartNew()
    31.     {
    32.         SceneManager.LoadScene(1);
    33.     }
    34.  
    35.     public void Exit()
    36.     {
    37.         if (UNITY_EDITOR)
    38.         {
    39.             EditorApplication.ExitPlaymode();
    40.         }
    41.        
    42.         else
    43.         {
    44.             Application.Quit();
    45.         }
    46.      
    47.     }
    48. }
    49.  
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,882
    Look again closely at the code in step 2 and then compare it with what you typed in. ;)
     
    Ryiah likes this.
  3. yackbruh

    yackbruh

    Joined:
    Sep 13, 2023
    Posts:
    3
    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.
     
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,882
    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)
    {
    }
     
    Bunny83 and Ryiah like this.
  5. yackbruh

    yackbruh

    Joined:
    Sep 13, 2023
    Posts:
    3
    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. :D
     
    Ryiah and CodeSmile like this.
  6. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,997
    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.
     
    Last edited: Jan 29, 2024
    Bunny83, MaskedMouse and Ryiah like this.