- Home /
"The class named is not derived from MonoBehaviour or ScriptableObject!", although class is derived from MonoBehaviour
I keep getting the error "The class named 'OldManSounds' is not derived from MonoBehaviour or ScriptableObject!". I have researched this, and seen that the solution is usually to add ": MonoBehaviour" to the class declaration. However, my class declaration is as follows:
public class OldManSounds : MonoBehaviour {
yet I still receive the error, even though I'm stating that it's derived from MonoBehaviour. How can I fix this?
It would help to see the entire script. I assume you have "using UnityEditor" at the top.
Here's the full script:
using UnityEngine;
using UnityEditor;
using System.Collections;
public class Old$$anonymous$$anSounds : $$anonymous$$onoBehaviour {
AudioSource audio1; AudioSource audio2;
public AudioSource[] aSources;
void Start()
{
aSources = GetComponents<AudioSource>();
audio1 = aSources[0];
audio2 = aSources[1];
}
void OnCollisionEnter(Collision collision) {
foreach (ContactPoint contact in collision.contacts) {
Debug.DrawRay(contact.point, contact.normal, Color.white);
}
if (collision.relativeVelocity.magnitude > 2)
audio1.Play();
if (collision.relativeVelocity.magnitude < 2)
audio2.Play();
}
}
Where are you getting this error? On the console? Can you upload a picture with the error in question?
Are you sure that you don't have other real errors? If there are errors in one of your scripts non of the scripts will compile / work until you fix all errors. Errors have a red icon while warnings are yellow.
Answer by ISeidingerReflekt · Dec 17, 2019 at 09:56 AM
If you are using assembly definition (.asmdef) files: Make sure the script is not limited to the editor platform
Answer by vintar · Jul 14, 2016 at 09:21 PM
Its as I said in the comment above. Remove the line "using UnityEditor" and all will be well ;)
Is the script in a folder called 'Editor"? That can also trigger this warning
I encountered the same issue with an old project I revisited. Turns out I had my script in editor folder. $$anonymous$$oving it out of there cleared the error. Thanks!
Your answer
Follow this Question
Related Questions
No Monobehaviour scripts in files 1 Answer
You are trying to create a MonoBehaviour using the new keyword. This is not allowed 2 Answers
C# Conception - Hide inherited members and functions 1 Answer
Problem with attaching scripts to objects 1 Answer
Object reference not set to an instance of an object in c# class. 1 Answer