Search Unity

What reasons could cause "script must derive from monobehaviour" when it already does? (Code shown)

Discussion in 'Scripting' started by nyscersul, Nov 26, 2018.

  1. nyscersul

    nyscersul

    Joined:
    Oct 17, 2018
    Posts:
    73
    So far an internet search suggests it could be that the name of the file and the name of the class defined within it must be the same. I also found that it may happen if a variable shares the name with a part of the namespace somewhere else used, but in this case all my variables seem unique, the names match, and it still throws the error.

    My script as stands is below... it worked fine, then suddenly throws this error when i tried to create a prefab - this script is in use, and working, in about 4 different gameobjects, and also all its options seem to work - those i define as randomly rotating do so, and those without rotate linearly as defined.

    So why is unity messing with me???

    *beats it with a stick*

    (Thanks in advance)

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class randomRotate : MonoBehaviour
    7. {
    8.  
    9.     [Tooltip("Randomise?")]
    10.     public bool randomiseSpeeds = false;
    11.     [Tooltip("Randomise new directions on the fly?")]
    12.     public bool randomiseSpeedsChange = false;
    13.     [Tooltip("Rotate Speed Factor, Used to directly modify the speed by division (speed = speed / speedFactor).")]
    14.     public float rotateSpeedFactor = 1;
    15.     [Tooltip("Rotate Speed, whilst randomising it is the maximum.")]
    16.     public int rotateSpeedX = 0;
    17.     [Tooltip("Rotate Speed, whilst randomising it is the maximum.")]
    18.     public int rotateSpeedY = 0;
    19.     [Tooltip("Rotate Speed, whilst randomising it is the maximum.")]
    20.     public int rotateSpeedZ = 0;
    21.     [Tooltip("Rotate Speed fluidity factor, whilst randomising it is the relative speed of directions changes. 1 = Violent change, 1000 = slow change.")]
    22.     public int rotateSpeedFluidity = 1000;
    23.     private System.Random rnd = new System.Random();
    24.     private float startRotateSpeedX = 0;
    25.     private float startRotateSpeedY = 0;
    26.     private float startRotateSpeedZ = 0;
    27.     public float currentRotateSpeedX = 0.0f;
    28.     public float currentRotateSpeedY = 0.0f;
    29.     public float currentRotateSpeedZ = 0.0f;
    30.  
    31.  
    32.     void Start()
    33.     {
    34.         if (randomiseSpeeds == true)
    35.         {
    36.             if (rotateSpeedX > 0)
    37.             {
    38.                 startRotateSpeedX = rnd.Next((0 - rotateSpeedX), (rotateSpeedX + 1));
    39.                 startRotateSpeedX = startRotateSpeedX / rotateSpeedFactor;
    40.                 currentRotateSpeedX = startRotateSpeedX / 100;
    41.             }
    42.             if (rotateSpeedX > 0)
    43.             {
    44.                 startRotateSpeedY = rnd.Next((0 - rotateSpeedY), (rotateSpeedY + 1));
    45.                 startRotateSpeedY = startRotateSpeedY / rotateSpeedFactor;
    46.                 currentRotateSpeedY = startRotateSpeedY / 100;
    47.             }
    48.             if (rotateSpeedX > 0)
    49.             {
    50.                 startRotateSpeedZ = rnd.Next((0 - rotateSpeedZ), (rotateSpeedZ + 1));
    51.                 startRotateSpeedZ = startRotateSpeedZ / rotateSpeedFactor;
    52.                 currentRotateSpeedZ = startRotateSpeedZ / 100;
    53.             }
    54.  
    55.             StartCoroutine(randomRotateEnum());
    56.         }
    57.         else
    58.         {
    59.             currentRotateSpeedX = rotateSpeedX / rotateSpeedFactor;
    60.             currentRotateSpeedY = rotateSpeedY / rotateSpeedFactor;
    61.             currentRotateSpeedZ = rotateSpeedZ / rotateSpeedFactor;
    62.  
    63.         }
    64.     }
    65.     void Update()
    66.     {
    67.         Vector3 tempVector = new Vector3(currentRotateSpeedX, currentRotateSpeedY, currentRotateSpeedZ);
    68.         this.gameObject.transform.Rotate(tempVector);//, Time.deltaTime);
    69.  
    70.     }
    71.     IEnumerator randomRotateEnum()
    72.     {
    73.         bool rotatorActive = true;
    74.         while (rotatorActive == true)
    75.         {
    76.             if (randomiseSpeedsChange == true)
    77.             {
    78.                 if (rotateSpeedX > 0)
    79.                 {
    80.                     float tempRotateSpeedX = rnd.Next((0 - rotateSpeedX), (rotateSpeedX + 1));
    81.                     tempRotateSpeedX = tempRotateSpeedX / rotateSpeedFactor;
    82.                     currentRotateSpeedX = startRotateSpeedX + (tempRotateSpeedX / rotateSpeedFluidity);
    83.                 }
    84.                 if (rotateSpeedX > 0)
    85.                 {
    86.                     float tempRotateSpeedY = rnd.Next((0 - rotateSpeedY), (rotateSpeedY + 1));
    87.                     tempRotateSpeedY = tempRotateSpeedY / rotateSpeedFactor;
    88.                     currentRotateSpeedY = startRotateSpeedY + (tempRotateSpeedY / rotateSpeedFluidity);
    89.                 }
    90.                 if (rotateSpeedX > 0)
    91.                 {
    92.                     float tempRotateSpeedZ = rnd.Next((0 - rotateSpeedZ), (rotateSpeedZ + 1));
    93.                     tempRotateSpeedZ = tempRotateSpeedZ / rotateSpeedFactor;
    94.                     currentRotateSpeedZ = startRotateSpeedZ + (tempRotateSpeedZ / rotateSpeedFluidity);
    95.                 }
    96.             }
    97.             yield return new WaitForSeconds(0.5f);
    98.  
    99.  
    100.         }
    101.     }
    102.    
    103. }
    104.  
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    4,634
    Code (CSharp):
    1. public class randomRotate : MonoBehaviour
    We usually name classes with Pascal case: RandomRotate. And you should have this class in a file named the same: RandomRotate.cs.

    (This problem usually occurs when the file name and the class name aren't the same)
     
    vedram and Joe-Censored like this.
  3. nyscersul

    nyscersul

    Joined:
    Oct 17, 2018
    Posts:
    73
    I will go try this now, but, i am almost 100% sure i already tried that. Lets watch me go prove myself wrong. Thanks!
     
  4. nyscersul

    nyscersul

    Joined:
    Oct 17, 2018
    Posts:
    73
    Just tried it...

    "public class RandomRotate : MonoBehaviour"
    and
    "RandomRotate.cs"

    and i get the same again.

    You are trying to replace or create a prefab from the instance 'sunSphere' that contains the script 'RandomRotate', which is does not derive from MonoBehavior. This is not allowed.
    Please change the script to derive from MonoBehavior or remove it from the game object.
    UnityEditorInternal.InternalEditorUtility:projectWindowDrag(HierarchyProperty, Boolean)
    UnityEngine.GUIUtility:processEvent(Int32, IntPtr)
     
  5. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    9,890
    Do you have another RandomRotate class defined in your project?

    What version of Unity are you using? There are known bugs in certain versions, particularly 2018.3 betas.
     
  6. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    4,634
  7. nyscersul

    nyscersul

    Joined:
    Oct 17, 2018
    Posts:
    73
    2018.2.13f, and i have copied the script as a backup, but, i also changed the filename/classname. Ill just delete the other one, its identical anyways.
     
  8. nyscersul

    nyscersul

    Joined:
    Oct 17, 2018
    Posts:
    73
    And i dont think so i havent used the prefab editor, i edit them as an instance in the scene and drag them to the project.
     
  9. nyscersul

    nyscersul

    Joined:
    Oct 17, 2018
    Posts:
    73
    Just checked, not a case of another "RandomRotate" class. I changed the name and filename to a ridiculously disambiguous name, same error.
     
  10. nyscersul

    nyscersul

    Joined:
    Oct 17, 2018
    Posts:
    73
    Could the IENumerator be clashing? its the only ambiguous name? ("rotate")
     
  11. nyscersul

    nyscersul

    Joined:
    Oct 17, 2018
    Posts:
    73
    Filename = RandomRotateOrLinear.cs
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System;
    4. using System.Collections;
    5. public class RandomRotateOrLinear : MonoBehaviour
    6. {
    7.     [Tooltip("Randomise?")]
    8.     public bool randomiseSpeeds = false;
    9.     [Tooltip("Randomise new directions on the fly?")]
    10.     public bool randomiseSpeedsChange = false;
    11.     [Tooltip("Rotate Speed Factor, Used to directly modify the speed by division (speed = speed / speedFactor).")]
    12.     public float rotateSpeedFactor = 1;
    13.     [Tooltip("Rotate Speed, whilst randomising it is the maximum.")]
    14.     public int rotateSpeedX = 0;
    15.     [Tooltip("Rotate Speed, whilst randomising it is the maximum.")]
    16.     public int rotateSpeedY = 0;
    17.     [Tooltip("Rotate Speed, whilst randomising it is the maximum.")]
    18.     public int rotateSpeedZ = 0;
    19.     [Tooltip("Rotate Speed fluidity factor, whilst randomising it is the relative speed of directions changes. 1 = Violent change, 1000 = slow change.")]
    20.     public int rotateSpeedFluidity = 1000;
    21.     private System.Random rnd = new System.Random();
    22.     private float startRotateSpeedX = 0;
    23.     private float startRotateSpeedY = 0;
    24.     private float startRotateSpeedZ = 0;
    25.     public float currentRotateSpeedX = 0.0f;
    26.     public float currentRotateSpeedY = 0.0f;
    27.     public float currentRotateSpeedZ = 0.0f;
    28.  
    29.  
    30.     void Start()
    31.     {
    32.         if (randomiseSpeeds == true)
    33.         {
    34.             if (rotateSpeedX > 0)
    35.             {
    36.                 startRotateSpeedX = rnd.Next((0 - rotateSpeedX), (rotateSpeedX + 1));
    37.                 startRotateSpeedX = startRotateSpeedX / rotateSpeedFactor;
    38.                 currentRotateSpeedX = startRotateSpeedX / 100;
    39.             }
    40.             if (rotateSpeedX > 0)
    41.             {
    42.                 startRotateSpeedY = rnd.Next((0 - rotateSpeedY), (rotateSpeedY + 1));
    43.                 startRotateSpeedY = startRotateSpeedY / rotateSpeedFactor;
    44.                 currentRotateSpeedY = startRotateSpeedY / 100;
    45.             }
    46.             if (rotateSpeedX > 0)
    47.             {
    48.                 startRotateSpeedZ = rnd.Next((0 - rotateSpeedZ), (rotateSpeedZ + 1));
    49.                 startRotateSpeedZ = startRotateSpeedZ / rotateSpeedFactor;
    50.                 currentRotateSpeedZ = startRotateSpeedZ / 100;
    51.             }
    52.  
    53.             StartCoroutine(RandomRotator());
    54.         }
    55.         else
    56.         {
    57.             currentRotateSpeedX = rotateSpeedX / rotateSpeedFactor;
    58.             currentRotateSpeedY = rotateSpeedY / rotateSpeedFactor;
    59.             currentRotateSpeedZ = rotateSpeedZ / rotateSpeedFactor;
    60.  
    61.         }
    62.     }
    63.     IEnumerator RandomRotator()
    64.     {
    65.         bool active = true;
    66.         while(active=true)
    67.         {
    68.             if (randomiseSpeedsChange == true)
    69.             {
    70.                 if (rotateSpeedX > 0)
    71.                 {
    72.                     float tempRotateSpeedX = rnd.Next((0 - rotateSpeedX), (rotateSpeedX + 1));
    73.                     tempRotateSpeedX = tempRotateSpeedX / rotateSpeedFactor;
    74.                     currentRotateSpeedX = startRotateSpeedX + (tempRotateSpeedX / rotateSpeedFluidity);
    75.                 }
    76.                 if (rotateSpeedX > 0)
    77.                 {
    78.                     float tempRotateSpeedY = rnd.Next((0 - rotateSpeedY), (rotateSpeedY + 1));
    79.                     tempRotateSpeedY = tempRotateSpeedY / rotateSpeedFactor;
    80.                     currentRotateSpeedY = startRotateSpeedY + (tempRotateSpeedY / rotateSpeedFluidity);
    81.                 }
    82.                 if (rotateSpeedX > 0)
    83.                 {
    84.                     float tempRotateSpeedZ = rnd.Next((0 - rotateSpeedZ), (rotateSpeedZ + 1));
    85.                     tempRotateSpeedZ = tempRotateSpeedZ / rotateSpeedFactor;
    86.                     currentRotateSpeedZ = startRotateSpeedZ + (tempRotateSpeedZ / rotateSpeedFluidity);
    87.                 }
    88.             }
    89.             yield return new WaitForSeconds(0.5f);
    90.  
    91.  
    92.         }
    93.     }
    94.     private void Update()
    95.     {
    96.         Vector3 tempVector = new Vector3(currentRotateSpeedX, currentRotateSpeedY, currentRotateSpeedZ);
    97.         this.gameObject.transform.Rotate(tempVector);//, Time.deltaTime);
    98.  
    99.     }
    100. }
    101.  
     
  12. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    9,890
    I copy-pasted that script, and it works in 2018.2.17f1. I can add it to a scene GameObject, make a prefab of that scene GameObject, and also add it to an existing prefab.

    Does your script's filename perhaps have a blank space hiding in it?
     
  13. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    4,634
    Are you sure the file on the prefab and the file you edit are the same? Have you tried to recreate your prefab? (Reattach the edited file of course)
     
  14. nyscersul

    nyscersul

    Joined:
    Oct 17, 2018
    Posts:
    73
    You can stop - thankyou very much for your help, and attempts to continue helping.

    Its a unity bug, quite clearly.

    Ill explain...
     
  15. nyscersul

    nyscersul

    Joined:
    Oct 17, 2018
    Posts:
    73
    I added the script same way as i always do, by creating the file as an independent asset, then later adding it to the instance via a drag and drop to the intended item in the hierarchy.

    I got so annoyed, i decided to rewrite the entire class definition part, and copy the body of it into a new script after backing up the contents via notepad and deleting the old scripts then creating a new file.

    Before this point, i had attempted to drag the new file and it gave me the same error... but, in spite of my careful name changes etc, it reported i was attempting to add a different script from what i had selected, at this point it said a different similar script, one i had attempted to use as a backup... hence the choice to delete all the related scripts and create a new one. Which i did...

    The new script, in spite of being the clear and only reference still resulted in the same error, except this time, it also tried to reference a different script when dragging and dropping, which resulted in a script the compiler and game activity both proved is perfectly acceptable, so, its NOT the script, it is unity being a bit of a .... *insert inappropriate noun here*

    I use visual studio, and, thus far, due to its integrations with unity and offering the references to namespaces and even the descriptions of the included functions and the like, it has not failed to lead me to the culprit of every problem via its wiggly error lines. This time, it reported the script was fine... because it was. There is NOTHING wrong. So, i loaded it into a different project... Et voila, no errors.

    Fortunately, my workflow required the creation of a prefab because i was exporting a completed asset for a different project.

    The script now exists without any changes and is working perfectly in the new project.

    I think the error is related to the fact unity has a messed up cache in the old project, and it seems it couldnt tell which script was which, and other things may have been getting mixed up behind the scenes. Maybe it was a metadata corruption issue... Fortunately that project is an experimental sandbox... and its produce is script only, now, so i can render it inoperable and move on.

    Thanks again!
     
  16. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    4,634
    If you think it is a Unity bug, please open a bug report on it. Give Unity a chance to explore it and fix it if it's indeed a bug.
     
  17. nyscersul

    nyscersul

    Joined:
    Oct 17, 2018
    Posts:
    73
    I am unsure how to reproduce it, i have not tried, and it is also entirely possible i created the issue somehow, maybe i messed with the metadata without realising. If i manage to reproduce it i will submit a bug report.
     
    Lurking-Ninja likes this.
  18. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,579
    Delete the library folder. Then do reimport all.

    It sounds like you may have messed up the meta data file for the script. Unity doesn't access scripts by name, but by their ID in the meta file. So if you did some weird stuff with renaming files, its possible the metadata is pointing somewhere other then where you think it is.

    Alternatively just replace the reference to the script on the prefab by dragging and dropping again.
     
    SparrowsNest likes this.
  19. mr_blahblah

    mr_blahblah

    Joined:
    Jan 15, 2016
    Posts:
    25
    Having the same issue with 2018.3.11f1 - just chiming in to report that if you the script you're dragging and dropping in is an Abstract class, Unity complains. Not sure if that's related to the original issue or not...
     
  20. RACHITSINGH433

    RACHITSINGH433

    Joined:
    Oct 17, 2018
    Posts:
    3
    I was also facing same problem :"(

    It may seems weird but how mine was solved is - I have errors in other scripts which I knew that I have to solve
    And then after that errors this one also started poping up

    But when I solved errors of another scripts it also got solved.
    Don't know how it happened but I got it fixed this way :")