Search Unity

The Script needs to Derive From MonoBehavior????

Discussion in 'Editor & General Support' started by TheWittyRobin, Oct 14, 2018.

Thread Status:
Not open for further replies.
  1. TheWittyRobin

    TheWittyRobin

    Joined:
    Aug 18, 2018
    Posts:
    36
    Okay guys im gettting kinda heated at this error message especially since i just updated the engine thinking it would fix this. See the thing that gets me heated is that im trying to add the script indicated at the bottom titled FadeWhite but it gives me the error for a different script. Not to mention the error is claiming its not deriving from monobehavior although it clearly is unless i missed something. Does anynone else get this? Have you found a fix for it? Are you a dev? Cause guys this is happening with just about every single one of my scripts now since i updated and even before then i had to reuse old scripts to avoid this. monoBehavior.png mono.png
     
  2. It works for me. Is it possible that you have name duplication and the other class isn't derived from MonoBehavoiur?
    If you create a brand new script with nothing in it just the class derived from MB, can you attach it?
    Do you have any errors in the console?
     
    Ishidres likes this.
  3. TheWittyRobin

    TheWittyRobin

    Joined:
    Aug 18, 2018
    Posts:
    36
    see i dont have a problem with that but how do i know what doesnt derive from monobehavior?
     
  4. I know two things which trigger this message:
    - when the class isn't derived from MonoBehaviour (the ': MonoBehaviour' part in the class declaration)
    - when the class isn't in the same named .cs file so the LevelManager class should be in the LevelManager.cs file

    Now, I know about these two, but it does not mean there aren't more circumstances when similar message occurs.
     
  5. Deleted User

    Deleted User

    Guest

  6. Takeaway: always use namespace.
     
    JWLewis777 likes this.
  7. TheWittyRobin

    TheWittyRobin

    Joined:
    Aug 18, 2018
    Posts:
    36
    well see that doenst explain why its giving me an error fro a completely different script.
     
  8. roykoma

    roykoma

    Joined:
    Dec 9, 2016
    Posts:
    176
    You say the error appears when adding the FadeWhite.cs script, so the error is very likely in that file. Please post the content of it, or at least check of it's class name is "FadeWhite".
     
    DMcaspar likes this.
  9. TheWittyRobin

    TheWittyRobin

    Joined:
    Aug 18, 2018
    Posts:
    36
  10. roykoma

    roykoma

    Joined:
    Dec 9, 2016
    Posts:
    176
    The 'w' in the class name is lowercase. You have to write it exactly as the file name, so "FadeWhite".
     
    Gracelove4 and user099 like this.
  11. TheWittyRobin

    TheWittyRobin

    Joined:
    Aug 18, 2018
    Posts:
    36
    Alright that fixed this one.
     
  12. RuthSanchoHuerga

    RuthSanchoHuerga

    Joined:
    Mar 9, 2019
    Posts:
    1
    Hi,
    I have the same exact problem. The name is the same in both, script. Public Class is MonoBehaviour,
    I had create the script in the Assets folders, And it says can add scriptbehaviour SerialThread. The script need to derive from Monobehaviour! But it is MonioBehaviour and I do not know what else I could do, can anybody help me, please?
     
  13. roykoma

    roykoma

    Joined:
    Dec 9, 2016
    Posts:
    176
    Hi @RuthSanchoHuerga

    could you please show the code of your script and preferably a screenshot of the script in the editor (Like TheWittyRobin did in his post). This will help us to identify the issue.
    If you have any errors showing in the console, please show these too.

    Greetings,
    Roy
     
  14. Symbology

    Symbology

    Joined:
    Dec 10, 2015
    Posts:
    3
    upload_2019-5-6_19-20-50.png

    upload_2019-5-6_19-21-17.png

    Same error. The fix was actually the fact that I had used ' instead of " in a Debug.Log...... Just FYI always double check your console!
     
  15. GoodMath

    GoodMath

    Joined:
    Sep 23, 2019
    Posts:
    3
    My script is having the same problem:

    The Script itself is called PlayerMovement

    using UnityEngine;
    public class PlayerMovement : MonoBehaviour
    {
    public Rigidbody rb;
    public float sidewaysforce = 500;
    public float forwardforce = 500;

    void FixedUpdate ()
    {
    if ( Input.GetKey("d") )
    {
    rb.AddForce(sidewaysforce * Time.deltaTime,0,0);
    }
    if ( Input.GetKey("a") )
    {
    rb.AddForce(-sidewaysforce * Time.deltaTime,0,0);
    }
    if ( Input.GetKey("w") )
    {
    rb.AddForce(0,0,forwardforce * Time.deltaTime);
    }
    if ( Input.GetKey("s") )
    {
    rb.AddForce(0,0,-forwardforce * Time.deltaTime);
    }
    }
    }
     
    S3LCSUM likes this.
  16. Please:

    - open a new thread when you have a question
    - please use CODE tags as it is described here: https://forum.unity.com/threads/using-code-tags-properly.143875/ (see below)
    - describe what is "the same" problem?
    - what is the content of your console?
    - make sure that the class name (PlayerMovement) and the file name you're using (should be PlayerMovement.cs) are exactly the same

    Code (CSharp):
    1. using UnityEngine;
    2. public class PlayerMovement : MonoBehaviour
    3. {
    4.     public Rigidbody rb;
    5.     public float sidewaysforce = 500;
    6.     public float forwardforce = 500;
    7.  
    8.     void FixedUpdate ()
    9.     {
    10.         if ( Input.GetKey("d") )
    11.         {
    12.             rb.AddForce(sidewaysforce * Time.deltaTime,0,0);
    13.         }
    14.         if ( Input.GetKey("a") )
    15.         {
    16.             rb.AddForce(-sidewaysforce * Time.deltaTime,0,0);
    17.         }
    18.         if ( Input.GetKey("w") )
    19.         {
    20.             rb.AddForce(0,0,forwardforce * Time.deltaTime);
    21.         }
    22.         if ( Input.GetKey("s") )
    23.         {
    24.             rb.AddForce(0,0,-forwardforce * Time.deltaTime);
    25.         }
    26.     }
    27. }
     
  17. bbtecson077

    bbtecson077

    Joined:
    Sep 9, 2019
    Posts:
    1
    I got the same error and I changed the class name since it didn't match with the .cs file. It worked :) thank you!
     
  18. BlueDragoness

    BlueDragoness

    Joined:
    Mar 26, 2020
    Posts:
    2
    I was getting this error for trying to attach any new script, but the existing scripts would attach. The fix ended up being that I had a compile error in an existing script. It was a totally different script! But, after fixing this error my new scripts are attaching just fine. :)
     
  19. Sharkbyteprojects

    Sharkbyteprojects

    Joined:
    Feb 16, 2019
    Posts:
    1
    Try to update all Packages with the Package Manager (Window->Package Manager).
    This work if you had updated to newer version of unity and before no errors
     
  20. MosquitoByte

    MosquitoByte

    Joined:
    Sep 17, 2018
    Posts:
    213
    This has happened to me recently as well, except my script actually DIDN'T derive from monobehavior and that was on purpose. I ended up making it do so after all though, because it wouldn't let me attach the script unless i did. Curious as to why it is necessary, anyone know?
     
  21. JProgrammer12

    JProgrammer12

    Joined:
    Jan 30, 2021
    Posts:
    1
    I can't seem to get this playable script to work. Any ideas why?

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using EnityEngine.SceneManagement;
    public class NewPlayableAsset : MonoBehaviour
    [Header("Movement")]
    [SerializeField] float movSpeed; 1
    [SerializeField] float airMovSpeed; 1
    [SerializeField] float movAccel; 1
    [SerializeField] float airMovAccel; 1
    [Header("Ground detection")]
    [SerializeField] float groundCastRadius; 1
    [SerializeField] float groundCastDist; 1
    [SerializeField] ContactFilter2D groundFilter;
    new Rigidbody2D rigidbody;
    bool isGrounded;
    bool DoGroundCheck()
    {
    return DoGroundCast() != Vector2.zero;
    }
    Vector2 DoGroundCast()
    {
    RaycastHit2D[] hits = new RaycastHit2D[2];
    if (Physics2D.CircleCast(transform.position, groundCastRadius, Vector3.down, new ContactFilter2D(), hits, groundCastDist) > 1)
    {
    return hits[1].normal;
    }
    return Vector2.zero;
    }
    void Move(Vector2 _dir)
    {
    Vector2 velocity = rigidbody.velocity;
    Vector2 groundDir = Vector2.Perpendicular(DoGroundCast()).normalized;
    groundDir.x *= -1;
    Vector2 targetVelocity = groundDir * _dir * (isGrounded ? movSpeed : airMovSpeed);
    Vector2 velocityDelta = targetVelocity - velocity;
    float maxDelta = isGrounded ? movAccel : airMovAccel;
    velocityDelta.x = Mathf.Clamp(velocityDelta.x, -maxDelta, maxDelta);
    velocityDelta.y = 0;
    rigidbody.AddForce(velocityDelta * rigidbody.mass, ForceMode2D.Impulse);
    }
    void Start()
    {
    rigidbody = GetComponent<Rigidbody2D>();
    }
    void Update()
    {
    isGrounded = DoGroundCheck();
    Move(new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")));
    }
     
  22. tarod-net

    tarod-net

    Joined:
    May 29, 2015
    Posts:
    8
    Old post, but I solved this problem using the Help menu and then Reset Packages To Defaults.
     
  23. xqtr123

    xqtr123

    Joined:
    Jun 7, 2014
    Posts:
    20
    My filename and my class name was the same but I got this error message. What solved it was to restart Unity...
     
  24. jardapsk

    jardapsk

    Joined:
    Jan 7, 2019
    Posts:
    1
    Thanks!
     
  25. srbobileste

    srbobileste

    Joined:
    May 9, 2021
    Posts:
    1
    (classe public LevelManager : MonoBehaviour)
    e o nome do seu Script esta "FadeWhite", Coloque FadeWhite no lugar do LevelManager
     
Thread Status:
Not open for further replies.