- Home /
Cannot add component which is a script
I write the code to Instantiate like this:
monsterInstance.AddComponent<Script.MonsterFollowing>();
Visual Studio doesn't warn any error, but in Unity, it said:
The type or namespace name 'MonsterFollowing' does not exist in the namespace 'Script'
I created the class MonsterFollowing in the namespace and everything correctly. Code if you don't trust me:
using UnityEngine;
namespace Script
{
public class MonsterFollowing : MonoBehaviour
{
public float maxSpeed = 20f;
// Start is called before the first frame update
// Update is called once per frame
void Update()
{
GameObject Player = GameObject.Find("Player");
transform.LookAt(Player.transform.position);
Vector3 direction = transform.forward;
Rigidbody rb = GetComponent<Rigidbody>();
if (rb.velocity.magnitude <= maxSpeed)
{
rb.AddForce(direction * 3, ForceMode.VelocityChange);
}
}
}
}
How can I fix this?
You can try using using Script, in the script you are calling it and try monsterInstance.AddComponent();
See Tsadriu's answer and comments which already address this point - the namespace.class
form is perfectly valid here.
Answer by Tsadriu · Sep 08, 2021 at 09:58 AM
Shouldn't it be like this?
monsterInstance.AddComponent<MonsterFollowing>();
I'm at work so I can't really test it.
No, MonsteringFollowing
is in the Script
namespace so the code should work (and is necessary unless one has a using Script;
declaration at the top)
Answer by Bonfire-Boy · Sep 08, 2021 at 11:03 AM
The most common cause of this is that your script file does not have the same name as the class it contains.
Each Monobehaviour class should be in a .cs file with exactly the same name (i.e. case sensitive) as the class (and hence also only one such class per file).
If not, the engine doesn't recognise them as Monobehaviours (but it's just a Unity thing hence VS not complaining). Same goes for ScriptableObject classes.
So in this case, make sure your script file is named MonsterFollowing.cs
.
Yes, the file name is correct. Also, VS can already recognize the class (hover the mouse to MonsterFollowing, it shows: class Script.MonsterFollowing), not Unity though
Answer by Bunny83 · Sep 08, 2021 at 04:00 PM
Well, even if the filename doesn't match the class name, the code should compile anyways. Since you get a compiler error that the class does not exist, I'm pretty sure you try to access this class from a lower compilation group. So you may have placed your "Script.MonsterFollowing" script somwhere inside your Assets folder but you try to access if from inside the Standard Assets or Plugins folder, or from a seperate assembly (assembly definition file?) that comes before the normal compilation group. So of course such a script can not access something that does not exist yet, since the thing you try to access is compiled after the code that is trying to access it.
Since we don't see and can't possible know where the code is placed, this is of course just an assumption. Though based on the symptoms, this is the most likely issue.
@Bunny83 I assume you mean that I don't place my .cs file in the Asset folder? But anyway, I did place it in Asset, don't even know how to not place them in Asset. Also, I tried to remove the namespace as well, which also doesn't work.