Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  4. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Can't add script behaviour . The script needs to derive from MonoBehaviour!

Discussion in 'Extensions & OnGUI' started by Tom163, Mar 23, 2015.

  1. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,034
    I'm writing an editor extension that generates some game objects to populate the scene, and one of the steps is to attach a custom script to them.

    And I get the above error, often but not always - which puzzles me a lot. Needless to say, my script DOES derive from MonoBehaviour:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TM_PointData : MonoBehaviour {
    5.  
    6.     public int x=0;
    7.     public int y=0;
    8.     public bool connected = false;
    9.     public bool primary = false;
    10.  
    11. }
    12.  
    The code (in another script) to attach it is:
    Code (CSharp):
    1.                     GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
    2.                     sphere.transform.position = location;
    3.  
    4.                     // store some data for later use
    5.                     TM_PointData data = sphere.AddComponent<TM_PointData>();
    6.                     data.x = x;
    7.                     data.y = y;
    8.                     data.primary = primary;
    9.  

    Now as I said, sometimes it works, sometimes it doesn't, and I can't even figure out when. Anyone has an idea what's going wrong here?
     
  2. proandrius

    proandrius

    Unity Technologies

    Joined:
    Dec 4, 2012
    Posts:
    542
    It's weird, from what I see the code looks correct. Is that code below is an editor script? Maybe you can try writing additional typecast when adding/getting components, e.g.:
    Code (csharp):
    1. TM_PointData data = (TM_PointData) sphere.AddComponent<TM_PointData>();
    Most people just forget to add "using UnityEngine;" but in your case it seems something else.

    Anyway, as soon as you know more post again since it's kinda hard to figure out what is going on.
     
  3. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,034
    The weirdness continues. Following your answer I moved the script out of the Editor folder and suddenly everything works nicely. WTF?
     
  4. RPGCoder

    RPGCoder

    Joined:
    Feb 21, 2014
    Posts:
    13
    I'm seeing exactly the same thing. I'm trying to create a custom autosave process. I'm trying to get the Autosave object to instantiate during an EditorStartup class that has the [InitializeOnLoad] attribute so it fires as soon as the editor starts running. If my classes are in an Editor directory (which they should be) I get the 'script needs to derive from Monobehaviour' error when I try to add an Autosave component. If I move the scripts out of the Editor directory, it works. I've attached a minimal 4.6.3 project that displays the problem.
     

    Attached Files:

  5. rlab5762

    rlab5762

    Joined:
    May 28, 2018
    Posts:
    1
    I had the same problem..but the reason it was showing as a problem for me is when I saved my script name with a space in it. as soon as I realized and took the space out it worked fine..may not fix the problem for you. but it fixed my " script needs to derive from Monobehaviour" for me...so it may be in the way its named....just one solution out of many for whom may make the mistake I did..