How do ix this error?

I’m writing a script to make a variable in my script lerp on a button press. Here’s my script…

public class LerpExample : MonoBehaviour {
    float lerpTime = 1f;
    float currentLerpTime;
 
    float moveDistance = 10f;
 
    Vector3 startPos;
    Vector3 endPos;
 
    protected void Start() {
        startPos = MouseOrbitOTS.xOffset;
        endPos = MouseOrbitOTS.xOffset + transform.up * moveDistance;
    }
 
    protected void Update() {
        //reset when we press spacebar
        if (Input.GetButtonDown("Switch Shoulders")) {
            currentLerpTime = 0f;
        }
     
        //increment timer once per frame
        currentLerpTime += Time.deltaTime;
        if (currentLerpTime > lerpTime) {
            currentLerpTime = lerpTime;
        }
     
        //lerp!
        float perc = currentLerpTime / lerpTime;
        MouseOrbitOTS.xOffset = Vector3.Lerp(startPos, endPos, perc);
    }
}

But I keep getting this error even though ‘MonoBehaviour’ is clearly stated at the beginning…

Assets/SwithShoulders.cs(1,28): error CS0246: The type or namespace name `MonoBehaviour’ could not be found. Are you missing a using directive or an assembly reference?

Include the UnityEngine namespace or MonoBehaviour means nothing to the script.

1 Like

MonoBehaviour is part of UnityEngine. You need to add using UnityEngine to the top of your script.

Or you can use the fully qualified type name, but I wouldn’t suggest this.

You need to tell C# where the definition for MonoBehaviour is located. You do this with using statements at the top of the script. The one you’re looking for is UnityEngine. Simply put this at the top of your script.

using UnityEngine;

Alternatively you can specify the namespace when you use it. This is useful for situations where you only use one or two entries from the namespace. Or if two objects share the same name (ie UnityEngine.Object and System.Object).

public class LerpExample : UnityEngine.MonoBehaviour {
2 Likes