Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

The script 'Game' does not contain a class derived from UnityEngine.MonoBehaviur

Discussion in 'Editor & General Support' started by DmE0, Feb 15, 2021.

  1. DmE0

    DmE0

    Joined:
    Feb 14, 2021
    Posts:
    6
    Hey! Today I ran into this error: The script Game does not contain a class derived from UnityEngine.MonoBehaviour. I don't know what to do, I tried to change the name of the MonoBehaviour class to the name of the file in the script itself - it did not help. Below I will send a screenshot of the error and code. Hope someone can help.
     

    Attached Files:

  2. Vectorbox

    Vectorbox

    Joined:
    Jan 27, 2014
    Posts:
    232
    There are a number of errors in the 'Game' script which I've corrected below. For reference, compare the updated code with your original script then test it in your project.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6. public class Game : MonoBehaviour {
    7.     // --
    8.     public Transform ShopPan;
    9.     public Text MoneyText;
    10.     public int Money;
    11.     public int MoneyUp = 1;
    12.     private bool Check = true;
    13.  
    14.     // --
    15.     void Update() {
    16.         MoneyText.text = Money.ToString();
    17.     }
    18.     // --
    19.     public void OnClickButton() {
    20.         Money += MoneyUp;
    21.     }
    22.     // --
    23.     public void OnClickShopButton() {
    24.         if (Check == true) {
    25.             ShopPan.gameObject.SetActive(true);
    26.             Check = false;
    27.         }
    28.     }
    29.     // --
    30.     public void OnClickBuy() {
    31.         if (Money >= 100) {
    32.             Money *= 2;
    33.             Money -= 100;
    34.         }
    35.     }
    36. }
     
  3. DmE0

    DmE0

    Joined:
    Feb 14, 2021
    Posts:
    6
    Тhanks very much. I corrected the code, but the error persists. Most likely I need to reinstall Unity.
     
  4. Vectorbox

    Vectorbox

    Joined:
    Jan 27, 2014
    Posts:
    232
    Have you replaced the original script with the code above ?
     
  5. DmE0

    DmE0

    Joined:
    Feb 14, 2021
    Posts:
    6
    Yes of course
     
  6. atharfatahillah

    atharfatahillah

    Joined:
    Sep 23, 2020
    Posts:
    2
    i have also the same problem
     

    Attached Files:

  7. atharfatahillah

    atharfatahillah

    Joined:
    Sep 23, 2020
    Posts:
    2
  8. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    488
    Your script file is called
    walking
    , but your class is called
    ExampleClass
    . The class must be the same as the file name. So either change your class to be
    walking
    or change your filename to be
    ExampleClass
    .
    This is a forum, not a chat board. You have to waiting longer than 2 minutes for someone to help you.
     
    guesXy likes this.
  9. Seasheperd

    Seasheperd

    Joined:
    Apr 21, 2021
    Posts:
    4
    Code (CSharp):
    1. mit System.Collections;
    2. Verwendung von System.Collections.Generic;
    3. Verwendung von UnityEngine;
    4.  
    5. öffentliche Klasse 3DBewegung : MonoBehaviour
    6.  
    7. float horizontal
    8. [SerialzeField] float moveSpeed;
    9.  
    10. Start wird vor der ersten Frame-Aktualisierung aufgerufen
    11.     void Start()
    12.     {
    13.        
    14.     }
    15.  
    16. Update wird einmal pro Frame aufgerufen
    17.     void Update()
    18.     {
    19.        
    20.     }
    21. }
    22. festes Update
    23.  
    24. horizontal = Eingang. GetAxis("Horizontal")
    25.  
    26. if (horizontal < 0)
    27.     {
    28. Verwandeln. Übersetzen (Vector3.right * moveSpeed);
    29. }
    30. if (horizontal > 0)
    31. transform.translate (Vector3.left * moveSpeed);
    32.  
    33.  
     
  10. Seasheperd

    Seasheperd

    Joined:
    Apr 21, 2021
    Posts:
    4
    ES FUNKTIONIERT NICHT
     
  11. Seasheperd

    Seasheperd

    Joined:
    Apr 21, 2021
    Posts:
    4
    It doesn´ t works
     
  12. qwertuiop127245

    qwertuiop127245

    Joined:
    Apr 23, 2021
    Posts:
    1
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class movement : MonoBehaviour
    {
    public float speed;
    void Start()
    {

    }
    void Update()
    {
    Vector3 movementVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    transform.Translate(movementVector * Time.deltaTime * speed);
    }
    }
     
  13. Seasheperd

    Seasheperd

    Joined:
    Apr 21, 2021
    Posts:
    4
    Thanks A
     
  14. NoahLundie

    NoahLundie

    Joined:
    May 2, 2021
    Posts:
    2
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class DamagePlayer : MonoBehaviour
    {
    public LayerMask playerLayer;
    public gameObject player;
    void OnTriggerEnter2D(collider2D collider)
    {
    if (collider.IsTouchingLayers(playerLayer))
    {
    player.GetComponent<HealthSystem>().TakeDamage(5);
    }
    }
    }

    I have the same problem
     
  15. NoahLundie

    NoahLundie

    Joined:
    May 2, 2021
    Posts:
    2
    This is my error: The Script 'DamagePlayer' does not contain a class derived from UnityEngine.MonoBehavior
     
  16. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,913
    Please use proper code formatting on the forum. You can find how to do it here:
    https://forum.unity.com/threads/using-code-tags-properly.134625/

    Just look at the previous posts which you have hijacked, their code is readable, yours is a mess.

    With that said, do you have errors in the console besides this one?
     
  17. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    Capilatise the C in Collider2D

    Change
    collider2D
    to
    Collider2D
     
  18. galaxert

    galaxert

    Joined:
    Jun 8, 2021
    Posts:
    1
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class MapGenorater : MonoBehaviour
    {
    public int mapWidth;
    public int mapHeight;
    public float noiseScale;

    public void GenerateMap()
    {
    float[,] noiseMap = Noise.GenerateNoiseMap(mapWidth, mapHeight, noiseScale);


    }

    }
    this is my problem when i try to attach to script to a object it says does not contain a class derived from UnityEngine.MonoBehavior
     
  19. Ey20080524

    Ey20080524

    Joined:
    Jun 24, 2021
    Posts:
    1
    it says: the script 2d player controller does not contain a class derived from UnityEngine.MonoBehaviour
    can you help me please?
     

    Attached Files:

  20. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    488
    It's because your file is called
    2d player controller
    , but the class is
    NewBehaviourScript

    The filename and class name must match exactly (case sensitive as well), and you can't have spaces in your class name so be aware of that when you name your script file. When creating your script you should be naming it at that time as that sets the class name to match, however if you rename the script name it does not rename the class and you have to do that yourself.
     
  21. aloften

    aloften

    Joined:
    Aug 25, 2017
    Posts:
    18
    I found that this error also occurs sometimes when there is ANOTHER script with a compile-breaking error in it.

    Fixing any console errors will sometimes fix this. You can especially know if this is an issue by saving unity, closing it, and when reopening it take note if it tries to open in safe-mode. If it does, you might just have a few errors to fix in other scripts before it will compile. I noticed this when I installed a broken plugin.
     
    eeestrelok likes this.