[SOLVED] why am I getting error with "using UnityEngine.UI;" statement?

Hi, I’m currently following along in the video for lesson 30. I’m at the point where I’m creating the “public Text text” variable, and I noticed that the variable type “Text” wasn’t changing color, as indicated it would in the video, even after typing “using UnityEngine.UI” in the preamble at the top of the code (yes, I included the semicolons). I decided to run debugging and got several errors. I’m certain there are no typos in the code, and I’m hoping someone can help figure this out. These are the lines that are returning errors (I have a screenshot below showing this in the editor with the errors).

  • using UnityEngine;
  • using UnityEngine.UI;
  • public class TextController : MonoBehaviour {
  • public Text text;

OS:
I’m running Windows 10 Professional (on a PC).

UNITY:
I have both Unity 2017.1 and Unity 4.6.9f1 installed. For this lesson, I’m using the older 4.6.9f1, as suggested by the instructor. Note that I had already installed 2017.1 prior to taking this course, so a version of MonoDevelop had been installed prior to me installing 4.6.9f1. For that reason, I chose NOT to install MonoDevelop again while installing 4.6.9f1. Is there an incompatibility between the Unity 4.6.9f1 editor and the Unity 2017.1 version of MonoDevelop? If not, WTH is going on here?

OTHER INFO:
I got some similar errors when running with debug on Number Wizard, but it didn’t seem to impact the script being run successfully within Unity, so I don’t know how important this actually is. It is quite annoying though, regardless.

Thanks! Here’s a screenshot:

Looks like you have either opened a script, or written one, but outside of a project/solution. As such the referenced libraries (UnityEngine) are missing, or haven’t been loaded.

1 Like

yeah, i get that sometimes, but I tend to close monodevelop down, then reopen the script from within Unity Editor just to make sure all the references are linked.

1 Like

Thanks for the quick reply, Rob. I’ve attached a screenshot of my directory structure and Unity environment. (my game is called Escape instead of Prison), but everything else is the same so far, as I’ve been following along step by step, including the previous lesson for creating the new project and directory.

Thanks OboShape, I’ll try that and post back if it works.

[EDIT]

Just saved and closed the script in MonoDevelop, and saved and closed the project in Unity. Then reopened Unity, and double clicked the script in the assets pane, which opened the script in MonoDevelop. I’m still getting the same four errors.

Any other thoughts on what might be causing these errors? Can they be ignored, or will they eventually become a problem?

The issue isn’t with the structure of your Windows files / folders. It is here, from your original screenshot…

image

Under the Solution pane there is no solution, no project. These would hold the references to the other code libraries required to build your project - e.g. UnityEngine.

Because these libraries/references are missing, when you try to run TextController.cs it has absolutely no idea what a UnityEngine is, or anything else that references it or belongs to it…

Regarding whether it matters - yes, absolutely. Any errors, such as syntax or missing libraries like these, will prevent the code from compiling and your project will simply not run. Your code relies on, that is, has a dependency on, these other libraries.

So… how did you start editing this TextController.cs script? Did you open MonoDevelop and create a new file? Or did you create a new Script within Unity and then double-click it to launch MonoDevelop?


Updated Thu Sep 14 2017 20:20

This, for example, is a snippet from a .csproj file for one of the course games;

  <ItemGroup>
    <Reference Include="mscorlib" />
    <Reference Include="System" />
    <Reference Include="System.XML" />
    <Reference Include="System.Core" />
    <Reference Include="Boo.Lang" />
    <Reference Include="UnityScript.Lang" />
    <Reference Include="System.Runtime.Serialization" />
    <Reference Include="System.Xml.Linq" />
    <Reference Include="UnityEngine">
      <HintPath>Library\UnityAssemblies\UnityEngine.dll</HintPath>
    </Reference>
    <Reference Include="UnityEngine.UI">
      <HintPath>Library\UnityAssemblies\UnityEngine.UI.dll</HintPath>
    </Reference>
    <Reference Include="UnityEngine.Networking">
      <HintPath>Library\UnityAssemblies\UnityEngine.Networking.dll</HintPath>
    </Reference>
    <Reference Include="UnityEngine.TestRunner">
      <HintPath>Library\UnityAssemblies\UnityEngine.TestRunner.dll</HintPath>
    </Reference>
    <Reference Include="nunit.framework">
      <HintPath>Library\UnityAssemblies\nunit.framework.dll</HintPath>
    </Reference>
    <Reference Include="UnityEngine.Analytics">
      <HintPath>Library\UnityAssemblies\UnityEngine.Analytics.dll</HintPath>
    </Reference>
    <Reference Include="UnityEngine.HoloLens">
      <HintPath>Library\UnityAssemblies\UnityEngine.HoloLens.dll</HintPath>
    </Reference>
    <Reference Include="UnityEngine.VR">
      <HintPath>Library\UnityAssemblies\UnityEngine.VR.dll</HintPath>
    </Reference>
    <Reference Include="UnityEditor">
      <HintPath>Library\UnityAssemblies\UnityEditor.dll</HintPath>
    </Reference>
    <Reference Include="Facebook.Unity.Settings">
      <HintPath>Library\FacebookSDK\Facebook.Unity.Settings.dll</HintPath>
    </Reference>
  </ItemGroup>
  <ItemGroup>

As you can see, this project file holds the location information for where the other required libraries can be found.

In your first screenshot, where you have the yellow highlighting, can you move “up” a directory;

This PC > Documents > Repos > Unity > Escape

and provide another screenshot.

Couple of other minor things;

  • You dont need the /bin directory within your Assets within Unity
  • Consider creating a separate directory for your projects, such as D:\Dev\ or similar - eventually you will get clobbered by a “file path too long” issue.
1 Like

Hi Rob, thank you for all of this. To answer your question, I had MonoDevelop open, but created and launched the script from within Unity. I also tried it after closing MonoDevelop and Unity, relaunching Unity, and then opening the script from within Unity (with MonoDevelop still closed). The script opened in MonoDevelop, but still had the same errors.

I just recreated the lesson in Unity 2017.1 and I’m NOT getting any errors at all. I do have MonoDevelop selected as the default code editor in 4.6.9f1, but again it’s the MonoDevelop that was installed with 2017.1. I’m guessing the linkage problem is somehow with that? Or maybe a bug between disparate versions of the editor and MonoDevelop?

Regarding path name, thanks for the heads up. I’ll make a shorter path, something like C:\UnityDev\Repos\Escape.

1 Like

It could well be, I’ve never tried it. To be honest, I don’t use MonoDevelop at all, I use Visual Studio - the community edition is completely free. I would certainly recommend it.

There are a few little things that you get with it that just make things so much easier, most people starting out, if they don’t already come from a coding background often have issues with brackets/braces - especially closing them. Visual Studio has a nice little feature, by default, which highlights the corresponding brace/bracket - so you can see whether you have missed any or not really easily.

I guess the only true way to test the theory regarding the above would be to re-install the older version of Unity with MonoDevelop and then see if you can not recreate the issue.

That said, you will probably find it easier to just use the latest version of Unity to be honest. There are a few differences between Unity 5+ and the course material, these will also be true of Unity 2017. They are very well covered already through the forum posts, but primarily the differences are how scenes are loaded and the use of particle systems properties.

Also, if you remain with the 2017 version you will be able to build your games using WebGL which will then be able to be playd (more easily) by other people should you choose to share them.


See also;

2 Likes

Hi Rob, after moving my repo path, I ended up getting a strange error in Unity (should have taken a screenshot for posterity). In any case, I uninstalled/reinstalled v2017.1 and not getting any errors at all now. Thanks for all the pointers. I’ll bookmark the thread for later reference if needed.

2 Likes

Great to hear you are all sorted, well done :slight_smile:

1 Like

Privacy & Terms