- Home /
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);
}
Answer by Dave-Carlile · Jul 15, 2015 at 03:11 PM
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.
Thanks Dave. I think I understand the basics of namespaces and inheriting, and I use them a little bit in my project. But I think this has something to do with the scripts being in different folders. I just tried moving all the camera scripts from the Standard Assets folder to my own scripts folder and it worked! BUT I don't just want a quick fix. I want to learn and understand and do things the right way. So how can I solve this while still keeping the camera scripts in the Standard Assets folder?
Hmmm, Unity doesn't (shouldn't?) care what folder things are in when it comes to compiling code, so that seems odd.
There's probably no one right way to do it, but here are my thoughts... If I'm going to modify a standard asset, I'm going to create may own version of it and name it something different and probably put it in my namespace or use the default project namespace. $$anonymous$$odifying it in its original location risks having it overwritten if you do a package update.
If your code is referencing something that's in a different namespace it needs to either specifically use the namespace like I mentioned in the answer, or have a using
at the top that includes the namespace. If you end up with same named items in multiple namespaces then you must always be explicit when na$$anonymous$$g those. You can do aliasing with the `using` statement but I'm not sure if Unity supports that.
Ok, thank you for the reply! You make a great point about duplicating Standard Assets before modifying, so it sounds like I'm better off copying them over anyways. I've never had this problem before (I currently have scripts in multiple folders with no issues), so it must just be unique to the Standard Assets folder. I'll consider this question solved, but if anyone else has insight into this phenomenon then I'd welcome it.
Answer by cmikef · Apr 18, 2018 at 12:18 PM
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.
Thank you! Restart both didn't work for me, but I did move the file back and everything worked after that.
Answer by siscoartz · Feb 01, 2019 at 02:30 PM
I shut down Visual studios and reopened and it worked!
Answer by IssataySC · Jun 01 at 03:52 AM
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));