Class Provided Script Not Working, Need Help

So a little background, I’m looking to break into the game development market and I started by taking a few coursea classes. In one such class, we are not focused on programming but rather learning the basics of Unity’s interface. Unfortunately, one of scripts the class provided which is meant to allow for a controllable camera results in the CS0246 Error.

(1,7): error CS0246: The type or namespace name ‘UnityEngine’ could not be found (are you missing a using directive or an assembly reference?)
(4,29): error CS0246: The type or namespace name ‘MonoBehaviour’ could not be found (are you missing a using directive or an assembly reference?)
(6,6): error CS0246: The type or namespace name ‘TooltipAttribute’ could not be found (are you missing a using directive or an assembly reference?)
(7,12): error CS0246: The type or namespace name ‘GameObject’ could not be found (are you missing a using directive or an assembly reference?)
(10,12): error CS0246: The type or namespace name ‘GameObject’ could not be found (are you missing a using directive or an assembly reference?)

  • additional 19 errors

It’s probably a simple script but could anyone point me in the right direction when it comes to fixing it? Any help would be HUGELY appreciated

using UnityEngine;
using System.Collections;

public class LookAtTarget : MonoBehaviour {

    [Tooltip("This is the object that the script's game object will look at by default")]
    public GameObject defaultTarget; // the default target that the camera should look at

    [Tooltip("This is the object that the script's game object is currently look at based on the player clicking on a gameObject")]
    public GameObject currentTarget; // the target that the camera should look at

    // Start happens once at the beginning of playing. This is a great place to setup the behavior for this gameObject
    void Start () {
        if (defaultTarget == null)
        {
            defaultTarget = this.gameObject;
            Debug.Log ("defaultTarget target not specified. Defaulting to parent GameObject");
        }

        if (currentTarget == null)
        {
            currentTarget = this.gameObject;
            Debug.Log("currentTarget target not specified. Defaulting to parent GameObject");
        }
    }
   
    // Update is called once per frame
    // For clarity, Update happens constantly as your game is running
    void Update()
    {
        // if primary mouse button is pressed
        if (Input.GetMouseButtonDown(0))
        {
            // determine the ray from the camera to the mousePosition
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            // cast a ray to see if it hits any gameObjects
            RaycastHit[] hits;
            hits = Physics.RaycastAll(ray);

            // if there are hits
            if (hits.Length>0)
            {
                // get the first object hit
                RaycastHit hit = hits[0];
                currentTarget = hit.collider.gameObject;

                Debug.Log("defaultTarget changed to "+currentTarget.name);
            }
        } else if (Input.GetMouseButtonDown(1)) // if the second mouse button is pressed
        {
            currentTarget = defaultTarget;
            Debug.Log("defaultTarget changed to " + currentTarget.name);
        }

       // if a currentTarget is set, then look at it
        if (currentTarget!=null)
        {
            // transform here refers to the attached gameobject this script is on.
            // the LookAt function makes a transform point it's Z axis towards another point in space
            // In this case it is pointing towards the target.transform
            transform.LookAt(currentTarget.transform);
        } else // reset the look at back to the default
        {
            currentTarget = defaultTarget;
            Debug.Log("defaultTarget changed to " + currentTarget.name);
        }
    }
}

Does Unity itself report any errors?

If not, this is just Visual Studio being Visual Studio, and here’s how you can fix that:

This may help you with intellisense and possibly other Visual Studio integration problems:

Sometimes the fix is as simple as doing Assets → Open C# Project from Unity. Other times it requires more.

Other times it requires you also nuke the userprefs and .vsconfig and other crufty low-value high-hassle files that Visual Studio tends to slowly damage over time, then try the above trick.

Barring all that, move on to other ideas:

Also, try update the VSCode package inside of Unity: Window → Package Manager → Search for Visual Studio Code Editor → Press the Update button

Also, this: No suggestions in Vscode

1 Like

Thank you so much for commenting (P.S, I saw that you you help a lot with these posts which is admirable and kind)

To your question about if Unity reported any errors, it did,

NullReferenceException: Object reference not set to an instance of an object

I tried attempting to fix the issue in the editor but eventually I assumed that it was a problem with the code. I could be wrong since I’m VERY new to Unity.

I’m testing the great variety of your suggestions still trying to find a solution. Also I wanted to ask a newbie question

“Other times it requires you also nuke the userprefs and .vsconfig and other crufty low-value high-hassle files”
in that in reference to Unity or VS or both?

Thank you so much once again

How to fix a NullReferenceException error

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Steps to success:

  • Identify what is null
  • Identify why it is null
  • Fix that

It’s the files that Unity makes to tell VS how to start up. In regular VS development you care about those files. In Unity, they’re disposable: Unity remakes them anytime you want with the “Open C# Project” method, wiping out any changes you may have inadvertently made.

1 Like