Search Unity

"The script must be derived from Monobehaviour "

Discussion in 'Scripting' started by JKMS, Apr 26, 2015.

  1. JKMS

    JKMS

    Joined:
    Nov 1, 2014
    Posts:
    37
    I was making the character switcher script give in this video , I have done all the scripts accurately but this one is making me mad . I have 2 scripts , hey two have the same errors , Im giving those scripts , hope some one could fix it .
     

    Attached Files:

  2. JKMS

    JKMS

    Joined:
    Nov 1, 2014
    Posts:
    37
    Also i have tried adding "
    Code (CSharp):
    1. public class ControllerParameters2d : MonoBehaviour
    " , it dont work. :(
     
  3. JKMS

    JKMS

    Joined:
    Nov 1, 2014
    Posts:
    37
    Im trying it to attach the script to the player prefab to make the player move .
     
  4. HAlbera

    HAlbera

    Joined:
    Jun 7, 2013
    Posts:
    63
    If you are attaching a script to a GameObject it must inherit from MonoBehaviour.
     
  5. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. [Serializable]
    6. public class ControllerParameters2D: MonoBehaviour
    7. {
    8.     public enum JumpBehavior
    9.     {
    10.         CanJumpOnGround,
    11.         CanJumpAnywhere,
    12.         CantJump
    13.     }
    14.  
    15.     public Vector2 MaxVelocity = new Vector2(float.MaxValue, float.MaxValue);
    16.  
    17.     [Range(0, 90)]
    18.     public float SlopeLimit = 30;
    19.  
    20.     public float Gravity = -25f;
    21.  
    22.     public JumpBehavior JumpRestrictions;
    23.  
    24.     public float JumpFrequency = .25f;
    25.  
    26.     public float JumpMagnitude = 12;
    27. }
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ControllerState2D: MonoBehaviour
    5. {
    6.     public bool IsCollidingRight { get; set; }
    7.     public bool IsCollidingLeft { get; set; }
    8.     public bool IsCollidingAbove { get; set; }
    9.     public bool IsCollidingBelow { get; set; }
    10.     public bool IsMovingDownSlope { get; set; }
    11.     public bool IsMovingUpSlope { get; set; }
    12.     public bool IsGrounded { get { return IsCollidingBelow; } }
    13.     public float SlopeAngle { get; set; }
    14.  
    15.     public bool HasCollisions { get { return IsCollidingRight || IsCollidingLeft || IsCollidingAbove || IsCollidingBelow; } }
    16.  
    17.     public void Reset()
    18.     {
    19.         IsMovingUpSlope =
    20.             IsMovingDownSlope =
    21.             IsCollidingLeft =
    22.             IsCollidingRight =
    23.             IsCollidingAbove =
    24.             IsCollidingBelow = false;
    25.  
    26.         SlopeAngle = 0;
    27.     }
    28.  
    29.     public override string ToString()
    30.     {
    31.         return string.Format(
    32.             "(controller: r:{0} l:{1} a:{2} b:{3} down-slope:{4} up-slope: {5} angle: {6})",
    33.             IsCollidingRight,
    34.             IsCollidingLeft,
    35.             IsCollidingAbove,
    36.             IsCollidingBelow,
    37.             IsMovingDownSlope,
    38.             IsMovingUpSlope,
    39.             SlopeAngle);
    40.     }
    41. }
    Your scripts must derive from MonoBehaviour. If they do not you cannot attach them to a GameObject. These scripts should work if you copy paste them over. All I did was add a semicolon and 'MonoBehaviour' to them.
     
  6. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    I don't think either of these scripts is for more than just storing parameters though. Neither one of them has any sort of character motor in it. You might want to follow the tutorials a little further, because these scripts alone will not move your player.
     
  7. JKMS

    JKMS

    Joined:
    Nov 1, 2014
    Posts:
    37
    Hamsterbyte.LLC , I have 4 scripts , 2 of them was not working so i post it here . I will check the script whether it works or not .
     
    Last edited: Apr 30, 2015
  8. JKMS

    JKMS

    Joined:
    Nov 1, 2014
    Posts:
    37
    Thank you Hamsterbyte.LLC , it worked perfectly . You're a great scripter !
     
    Last edited: Apr 30, 2015
  9. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    I would say I know a thing or two. Glad I was able to help you out. Best of luck in all your future coding endeavours!