I’m creating a game using Unity3d 4.6 and c#. But for some reason I can’t add scripts to gameobjects etc. I only get the message in the included file.
I get that message if I try to add my own scripts or imported ones like sample assets beta. I even tried to create a completely new project but still the same error. Also, I can see the files, the inspector show the correct code and I can edit them.
You can’t add scripts when there are compile errors in your code… check the debug console at the bottom of the screen, clicking on it should bring up all the errors in your scripts…
It’s also worth noting that you can only attach classes that derive from MonoBehaviour to objects. You can call stuff or add objects from other scripts just fine but you can only attach MonoBehaviours.
That error is in another script and is caused by my problem. I have this line: GetComponent<FirstPersonController>().enabled = true; which give me this error: Assets/Scripts/PlayerNetworkMover.cs(11,26): error CS0246: The type or namespace name `FirstPersonController’ could not be found. Are you missing a using directive or an assembly reference? even though I do have that script.
It looks like you don’t have that script attached to the object. GetComponent gets a component with that name attached to the object. If there is not one it causes problems. You can’t enable a script that is not attached. Use the Add Component to add the script and then you can GetComponent it within your code later.
I know that but the problem is that when I try to attach it I get the error saying that the script doesn’t exist so I can’t attach it even though I can see the script in the project window and the code in the inspector. Also, I can edit the script but Unity still tells me that it doesn’t exist. This happen when I try to attach any script, both my own and imported ones.
Yes, they all inherit from MonoBehaviour. Some of the scripts are the first person controller from sample assets beta and photons networkview script which I could add before but then they stopped working, so I removed the component so I could readd them but then I got the message telling me the script didn’t exist. Also, if I click add component and search it won’t find any scripts
First make a new Scene and make a new script and GameObject, then try attaching it to a gameobject. See if it’s an issue with your Unity, or your scene. Also since this has been going on since yesterday I assume you have tried closing and opening unity as well as restarting your computer? It’s not often but I have encountered weirdness like that once or twice where a fresh reboot would fix things.
Unfortunantly I haven’t tried today because I’m not home so I haven’t tried after a reboot, however, I did try restarting unity with no success. I also created a completely new project but I had the same problem there so the scene isn’t the problem
Wow, almost sounds like something might be corrupt in the installation. I would try re-installing Unity. If that doesn’t work, then it’s beyond my help (At that point I would have to assume it could be any number of problems involving your OS) This is all based on assumption and is not a guarantee. Like I said try uninstalling then reinstalling first. That works quite often for a lot of things.
The error you are having is a namespace error. Unity doesn’t know what “FirstPersonController” is. Chances are that script doesn’t exist in your project, or you have a typo in the name somewhere.
But then why would he still be having issues when creating a new scene with a game object and a fresh script? That shouldn’t fail if it was a namespace error no?
If you have a script in your project that can’t be compiled due to an error, the error still persists even in a new scene. Now, if it is a whole now project with no other scripts, then that would be weird.
Thanks for the answers everyone. When I commented out the line causing an error I could add components again There is still one problem though. When I try to use this code: GetComponent<FirstPersonController>().enabled = true; I get this error: Assets/Scripts/PlayerNetworkMover.cs(11,26): error CS0246: The type or namespace name `FirstPersonController’ could not be found. Are you missing a using directive or an assembly reference? The FirstPersonController is in the folder and it’s attached to a gameObject but I can’t access it through code for some reason. Do anyone know a fix to this? I’m using the FirstPersonController from Sample Assets beta for Unity 4.6