Search Unity

C# scripts not inheriting from Mono

Discussion in 'Editor & General Support' started by jeffries7, Mar 21, 2017.

  1. jeffries7

    jeffries7

    Joined:
    Jun 25, 2014
    Posts:
    59
    When ever I create a c# script inside the editor it doesn't inherit from Monobehavior. This seems to have happened when I switched to using Perforce.

    Currently to get around the issue I'm having to create a class in VS then add in everything else.
    I'm connected to the server inside of Unity using the built in version control and also connected inside VS.

    Any help is appreciated.
     
  2. mikael_juhala

    mikael_juhala

    Joined:
    Mar 9, 2015
    Posts:
    247
    What does the script contain when you create it from the editor? Have you modified the script template files?
     
  3. jeffries7

    jeffries7

    Joined:
    Jun 25, 2014
    Posts:
    59
    Everything that a normal script uses. I haven't intentionally changed anything.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Name : MonoBehaviour
    5. {
    6. }
     
  4. mikael_juhala

    mikael_juhala

    Joined:
    Mar 9, 2015
    Posts:
    247
    But that class does inherit MonoBehaviour. What actually is the issue?
     
    MNNoxMortem likes this.
  5. jeffries7

    jeffries7

    Joined:
    Jun 25, 2014
    Posts:
    59
    When I create the script it says MonoBehavior but it's white and not blue/green. I'm able to attach the script to objects in Unity but I obviously don't have access to Mono features such as Gameobject, etc.
     
    bentheman147 likes this.
  6. jeffries7

    jeffries7

    Joined:
    Jun 25, 2014
    Posts:
    59
    Anybody else having the same issue? Is this something in the project configured incorrectly? As I said this has only been an issue since using Perforce.

    It doesn't happen 100% of the time.
     

    Attached Files:

  7. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    It's still inheriting, it's your editor that's broken. Seems like you're using Visual Studio?

    You should be getting a message up about "files has been modified externally" when you create a script from Unity. Make sure to select "reload all" in that dialogue.
     
    MNNoxMortem likes this.
  8. jeffries7

    jeffries7

    Joined:
    Jun 25, 2014
    Posts:
    59
    I am using Visual Studio.

    When this issue happened a few months back I used to reload all to temporarily fix the issue but now I don't even get the reload window pop up. I have found that in Unity preferences if I change the text editor to mono and back to VS its fixes it. Again this only a temporary work around.
     
  9. arthaqs

    arthaqs

    Joined:
    Dec 22, 2016
    Posts:
    3
    Have you guys found any solution? For me changing editor to MonoDevelop and back to Visual Studio is not working.
     
  10. bglassmaker_unity

    bglassmaker_unity

    Joined:
    Oct 21, 2017
    Posts:
    1
    Old thread but it doesn't seem to have the answer I needed and it seems to happen on every new script I add with VS open at the time. What I did that has worked so far was to close and reopen VS, this gets my new script in the Solution Explorer. Then I click the Refresh button at the top of the solutions explorer in VS to correct the problem with monobehaviour not being inherited and made my intellisense work again.
     
    jipjax likes this.
  11. Double-V

    Double-V

    Joined:
    Aug 13, 2016
    Posts:
    21
    It's an old thread, but I've found a solution that doesn't feel like a hack.
    When you have your script open in VS, you will notice it says miscellaneous file in the left top corner of the script window (the window with actual code). That means it doesn't recognize it as a part of the solution.

    You should do the following thing:
    - have that script active (highlighted blue)
    - go to File and pick from the list an option that says Move *path of the script*.cs into:
    - you will notice the miscellaneous changed to whatever you have your project called
     
    Nonanas, PhiloAmgad, Rewar07 and 8 others like this.
  12. Lockes_TheThief

    Lockes_TheThief

    Joined:
    Apr 28, 2014
    Posts:
    17
    I seem to be having an issue like this as well. In the code it is saying it is deriving from monobehaviour but it doesn't work when you drag the code to a gameobject, etc.
     
  13. Lockes_TheThief

    Lockes_TheThief

    Joined:
    Apr 28, 2014
    Posts:
    17
    Oh I fixed it. The file doesn't change the class name when you change the file.
     
  14. lorella124

    lorella124

    Joined:
    Aug 8, 2018
    Posts:
    2
    Thank you so much!!
    That worked.
     
    idangame and itsvanoss like this.
  15. S4NNY1

    S4NNY1

    Joined:
    Dec 11, 2017
    Posts:
    1
    For me the "Attach to Unity" button in Visual Studio worked, mono got default blue back.
     
  16. GrayDwarf

    GrayDwarf

    Joined:
    May 6, 2015
    Posts:
    9
    *edit* - See my next couple of posts for better solution.
    After following suggestions above, I ended up with my own workaround.
    - Go back into Unity.
    - Right click on script and choose: "Open C# Project"
    - A folder containing the solution and project should open.
    - Double click the .sln file to open it.
    - Your script can be found in the solution explorer window under Assets/Scripts (assuming that's where you put them).
    - After saving, your problem should be resolved. If you open C# Script files from unity, they should open in the solution with references working.

    However, if you open the file standalone again, the problem remains. I'm still working on a fix for that.
     
    Last edited: Nov 13, 2019
    Dustin0530 likes this.
  17. GrayDwarf

    GrayDwarf

    Joined:
    May 6, 2015
    Posts:
    9
    *edit* - See my next post for better solution.
    A slightly better solution to the one above is to go into Unity->Edit->Preferences..., External Tools and select 'External Scripe Editor' and browse for your .proj file. You'll need to change the drop down list next to file name from .exe to All Files (*.*) so it shows the csproj file. Select the sln or csproj file and click Open to associate it. Close out of the Preferences dialog and then open your script file from Unity. Now, when you double click on a script file in Unity, it will open the solution and project with references working. However, you may have to manually navigate to your preferred file once it opens. Better but still working on a proper fix.
     
    Last edited: Nov 13, 2019
  18. GrayDwarf

    GrayDwarf

    Joined:
    May 6, 2015
    Posts:
    9
    Seemed to have fixed it. Close any instances to Visual Studio before performing these steps just to be safe.

    #1 - Get the full path to Visual Studio (2017 in my case)
    • HowTo: Find the shortcut icon in your start menu and view the properties.You may have to open the files location and then right click and view properties.
    #2 - Copy the full file path to your clipboard. Should copy the entire string so you've got something like this but yours will likely be different. "E:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\devenv.exe"
    #3 - Go into Unity->Edit->Preferences..., External Tools and select 'External Script Editor' and paste the path to visual studio without the double quotes and click Open.
    #4 - You should see the friendly short name of Visual Studio (2017) for External Script Editor now.
    #5 - Close the Preferences dialog and try double clicking your script file. Should open directly into your project/solution to the selected file.
     
  19. dplanz

    dplanz

    Joined:
    Feb 14, 2020
    Posts:
    1
    Chiming in here...

    Came here trying to fix my issue with this. I ended up needing to install .Net Framework 4.7.2 and open the solution instead of just opening the C# file
     
  20. coldramen69

    coldramen69

    Joined:
    May 4, 2020
    Posts:
    1
    This worked for me

    (I'm using VS 2019 btw.)
     
  21. naderlabbad309

    naderlabbad309

    Joined:
    Feb 11, 2018
    Posts:
    32
    These solutions do not work But at a certain time Open C# Project and then open your script doubal click
     
  22. Bejumboom

    Bejumboom

    Joined:
    Sep 2, 2020
    Posts:
    1
    Thank you!!
     
  23. Dustin0530

    Dustin0530

    Joined:
    Sep 14, 2020
    Posts:
    2
    This Worked Thank You
     
  24. ramonfe

    ramonfe

    Joined:
    Nov 17, 2019
    Posts:
    2
    Thank you, this was the solution for me and I'm using VS2019
     
    Zurinna likes this.
  25. ramonfe

    ramonfe

    Joined:
    Nov 17, 2019
    Posts:
    2
    This fixed my issue in VS2019, awesome
     
    theDustpan likes this.
  26. Galactic_CakeYT

    Galactic_CakeYT

    Joined:
    Apr 28, 2020
    Posts:
    65
    I copied the code, and deleted the script. Then I created a new script with the EXACT SAME NAME as the one I deleted. I then just put all the code back into the script, and the MonoBehaviour. Unity seems to have a lot of these bugs, its kind of annoying.
     
  27. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,993
    How exactly is that a Unity bug? This thread was just about Visual Studio that is having issues. That's mainly because you haven't setup VS correctly, you haven't installed the Unity Tools add on in VS or you some how just opened a single C# file instead of the solution which Unity opens by default. None of those issues are related to Unity but to VS.
     
  28. Galactic_CakeYT

    Galactic_CakeYT

    Joined:
    Apr 28, 2020
    Posts:
    65
    Technically it's connecting Unity to VS. Its about Unity and VS, that's a Unity thing. VS works perfectly fine. It's also a little bit more difficult then need be. But this is my opinion :D
     
  29. Yoyoman_Blue6

    Yoyoman_Blue6

    Joined:
    Dec 11, 2020
    Posts:
    3
    This seems like the best solution for me but when I go to the "External Script Editor" drop window, I only have *Open by file extension* or Browse..

    I've been really struggling to overcome this issue and I'm just a beginner in all of those other solutions are way too complicated for me.. If you can maybe make a video of you doing the other solutions it would be amazing would love it if you could.
     
  30. MannUp

    MannUp

    Joined:
    Feb 4, 2021
    Posts:
    1
    Thank you
     
  31. Smokee8726

    Smokee8726

    Joined:
    Feb 22, 2021
    Posts:
    1
    Thank you so much it worked
     
  32. sas67uss

    sas67uss

    Joined:
    Feb 8, 2020
    Posts:
    81
    1. In the visual studio go to solution Explorer
    2. In Assets hierarchy look for exactly folder that your script placed on that . probably you can't see the script in its folder under solution explorer hierarchy !
    IF YES then
    3_1. Right_click on exactly the mentioned folder select Add > Existing Item look for missing script in your unity project and add it . one click on some place of your script on VS editor . every thing must be OK .
    ELSE IF you see the script
    3_2. Right_click on the script delete it and re_add it .
     
  33. ShravanS

    ShravanS

    Joined:
    Jul 3, 2021
    Posts:
    1
    The installer for unreal engine is not installed in Visual Studio. This is the reason why this is happening.
     
  34. mrchick450

    mrchick450

    Joined:
    Nov 16, 2021
    Posts:
    1
    thx
     
  35. jlakmal

    jlakmal

    Joined:
    Jun 9, 2022
    Posts:
    1
  36. therealcal

    therealcal

    Joined:
    Jul 20, 2022
    Posts:
    1
    Hey,

    Had this issue, in Unity, going to File -> Preferences -> External Tools, and then selecting Visual Studio as the external script editor, then reloading the script was enough to fix it for me. Hope that helps.
     
    JohnnyAfro likes this.
  37. brian-nielsen

    brian-nielsen

    Joined:
    Apr 18, 2018
    Posts:
    15
    For working specifically with Unity and Perforce the following worked for me:

    1. In the Perforce client (I'm using P4V) "Check Out" both the "Assembly-CSharp.scproj" and "(project name) project.sln"
    2. Restart your code editor. I'm using Visual Studio Code.