The type or namespace name could not be found. Are you missing a using directive or an assembly reference?

I found similar posts here and on StackExchange, but they were different from my problem and provided no solution. I need to know how to reference another script from inside this Unity StandardAssets.Cameras namespace. Normally, I can just create a private variable of the same type as the script I want to reference, then fill it using GetComponent. But for some reason when I try to use this same method with one of Unity’s camera scripts, I get the error message in the title.

Here’s the relevant part of the script. I’m getting an error when trying to access my “RiderDismount” script. (This is all in C#) My RiderDismount script is in the following location: Assets–>GG–>Scripts and the AbstractTargetFollower camera script is in Assets–>StandardAssets–>Cameras–>Scripts. Not sure if that’s relevant.

using System;
using UnityEngine;
using Rewired;

namespace UnityStandardAssets.Cameras
{
    public abstract class AbstractTargetFollower : MonoBehaviour
    {
        public enum UpdateType // The available methods of updating are:
        {
            FixedUpdate, // Update in FixedUpdate (for tracking rigidbodies).
            LateUpdate, // Update in LateUpdate. (for tracking objects that are moved in Update)
            ManualUpdate, // user must call to update camera
        }

        [SerializeField] protected Transform m_Target;            // The target object to follow
        [SerializeField] private bool m_AutoTargetPlayer = true;  // Whether the rig should automatically target the player.
        [SerializeField] private UpdateType m_UpdateType;         // stores the selected update type

        protected Rigidbody targetRigidbody;

		public int playerId = 0; // The Rewired player ID that this camera will be tied to
		public Player player; // The Rewired Player

		public float xInputAxis;
		public float yInputAxis;

		private RiderDismount riderDismount;  // ERROR HERE


        protected virtual void Start()
        {
            // if auto targeting is used, find the object tagged "Player"
            // any class inheriting from this should call base.Start() to perform this action!
            if (m_AutoTargetPlayer)
            {
                FindAndTargetPlayer();
            }
            if (m_Target == null) return;
            targetRigidbody = m_Target.GetComponent<Rigidbody>();
			riderDismount = transform.root.gameObject.GetComponent<RiderDismount>();  // AND ERROR HERE

			// Get the Rewired Player object for this player.
			player = ReInput.players.GetPlayer(playerId);
        }

Your custom classes (RiderDismount) are likely in their own namespace that the UnityStandardAssets.Cameras namespace doesn’t know about. In other words its not in a scope that it can see.

When declaring RiderDismount riderDismount you need to either include the entire namespace - e.g. MyProjectNamespace.RiderDismount riderDismount or include a using at the top that specifies your namespace.

You might also consider making your own class and just inheriting AbstractTargetFollower then adding on your functionality.

I think the original problem was that you moved the cs files to a different folder, and then did not restart Visual Studios and Unity. I had the same problem, and spent some time googling to find the answer. After reading this post, I tried quitting and restarting Visual Studios and Unity, and the problem went away. My guess is that one or both of them do not handle moving cs files that reference each other, to new directories well unless you restart after the move. I restarted them both, so I’m not sure which one was the cause.

I shut down Visual studios and reopened and it worked!

I have encountered the same error while trying to add script component to the gameobject on runtime. Fixed it by following this format:

GameObject.AddComponent(typeof(namespace.className));