Unable to attach script components

About 2 weeks ago I purchased a brand new iMac from the Apple store specifically to get more serious with my coding.

In two different classes now, both provided by GameDev.tv, when I download scripts and import them into unity, I run into a problem that is really getting to me. When I attempt to attach a script to an object, sometimes (yes, not all the time), I get an error. At the moment, this is happening when I attempt to attach CameraRaycast.CS to my Camera Arm. I have not edited the script in anyway, I’ve only imported it as requested by Ben, and am trying to attach it. Here is the error:

“Can’t add script behaviour VisualContainerAsset. The script needs to derive from MonoBehaviour!”

I’ve searched for hours, tried countless bug fixes as reported by others, uninstalled and reinstalled Visual Studio, Monobehaviour, etc. It still occurs. There is a line in the script that reads

“public class CameraRaycaster : MonoBehaviour”

I don’t know if something is off here, but I’m fairly new to programming and about at my wits end. Please help! I’m fast to respond and truly eager to get past this problem. When I took these courses on a PC I had no problems at all and finished one of the Unity courses in its entirety.

Hello Michael,

Can you post a screenshot of the error(s) from the console please, oh and which version of Unity are you using?

Incidentally, I note that you’ve mentioned the script’s filename is “CameraRaycast.cs” yet the class is called “CameraRaycaster”, these both need to be the same, so, if they are not, rename the script’s file name to “CameraRaycaster.cs”.

I believe the file names are correct but I’ve attached two screenshots to help determine that.

Looks like this could be related to these issues;

I would be happy to take a quick look if you can share your project with me, the forum will allow uploads of up to 10mb, but I suspect your project will be larger than that. If so, please use a service like Google Drive / Dropbox and share the URL in your reply.


Updated Wed Jun 13 2018 17:42

I have just launched 2018.1.0f1 and was able to produce your error, but only once so far, by renaming the filename of the script within Unity to a name that didn’t match the class name and then changing it back.

After the first time this occurred I was not able to reproduce the issue, despite repeating the same steps. I have tried a few combinations and still haven’t been able to. It could be that Unity is compiling the code faster than I can make more changes and thus the error isn’t occurring.

Share your project with me and I will take a quick look.


Updated Wed Jun 13 2018 17:55

Ok, I think I can reproduce this now.

  • create a script with any name, or leave the default
  • rename the file under Assets to something else
  • top right of the Inspector will show a grey area with a message in it about the script not finding a MonoBehaviour when the script is selected under Assets
    image
  • drag this script to the Main Camera and then the error appears
    image

If I rename the script back to the same name as the class name, and then drag it quickly to the Main Camera I am presented with the same error again. This is because Unity is still trying to compile the code, the little progress indicator is spinning in the bottom right-hand corner.

Once this completes, the message in the top of the Inspector disappears and dragging the script to the Main Camera works successfully.

Renaming of this script’s filename so that it doesn’t match the class name appears to be the issue. The error being thrown based on timing of actions performed verses Unity compilation.

In your specific case, I note you have a warning at the bottom, do you have any errors at all, e.g. where the compilation has failed? If the compilation has failed since the renaming of the script file, it will not have the corresponding names again until it can compile successfully.

I’m still fairly new to coding, but I will do my best to do all of your requests and follow instructions. Here is a list of everything under errors when I look there. I will upload the project to Google Drive. I never had this problem with PC, or perhaps it being a previous version of Unity.

When I attempted to upload the entire folder for the project, I got errors in regards to .temp files.

The rest of it, in its entirety, can be found here
https://drive.google.com/open?id=1hwWrI6ht5eajhR5WN-TnlmqLnLoLG3h6

There’s an error there in that screenshot. That would be stopping a recompile, and thus any changes in naming won’t have been applied, which is what I suspect is causing your original error message regarding the MonoBehaviour.

From memory, there is a Utility.cs script file, this contains the enum Layer and should look like this;

public enum Layer {
    Walkable = 8,
    Enemy = 9,
    RaycastEndStop = -1
}

This gets changed later on in the second section, but I’m not entirely sure where you are at the moment, but as your error is pointing to this I would start there.

After you make this change, hopefully the project will build and you renaming should take place successfully.

I copy pasted this, with a line of my own called Scenery_Objects (torches, etc) and it seems to have worked. The CameraRaycast.cs attached no problem. Strange, I had the Utility.cs type up exactly this way, but without RaycastEndStop, as in the lesson I don’t believe the file included that originally when attaching. Oh well, at least it worked!

If this is the CameraRaycast.cs from the downloadable resources then I think it may have been updated.

You needed to add the additional enum to correct the error which was complaining about the missing enum. This error was preventing the code from compiling, and, as such the renaming of the CameraRaycaster.cs file from being updated.

Once the error was resolved, the code could compile, the filename/class name issue was rectified through the re-compile and thus, the script attaches.

Phew… Good to know though should this ever come up again! :slight_smile:

Thank you so much Rob, I’m truly having a great time with this class!

I feel like it’s beyond my scope of knowledge, but it is really entertaining and has me captivated as my end all be all dream is to design my own RPG’s. I feel like I’m catching on quick in terms of modeling and what not, but the programming part is just me following along doing what I’m told, lol.

Maybe I’m just not a programmer… perhaps my strength lies somewhere else. :smiley:

1 Like

You’re very welcome Michael.

I wouldn’t be too hard on yourself, especially if this is your first course, the RPG course does assume some previous experience, e.g. follows on from the original Unity course where some of the other bits have already been covered.

Regarding programming, maybe, maybe-not, time will tell, but we all have different strengths/abilities, and it doesn’t mean it has to be all or nothing. It maybe you do what you are comfortable with and focus on the aspects you enjoy more personally and then call in other people to help with the other areas, whatever they maybe, programing, 3D modelling, level design, story etc etc… lots of areas to dabble in and experiment with, as long as you are enjoying yourself, which it sound as if you are, I’d say you’re heading in the right direction :slight_smile:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms