Can't add script bacause of class cannot be found
What does it mean Can't add script component ... because the script class cannot be found?
I'm trying to make two hydraulic cylinders to point at each other with a LookAt-script in a mechanical rigg. But I seem not to be able to put the script on the object to even begin with...
What sort of function does this script class serve? Trying to learn =)
The script I'm using comes from the LookAt-tutorial. And it looks like this:
using UnityEngine; using System.Collections;
public class CameraLookAt : MonoBehaviour { public Transform target;
void Update ()
{
transform.LookAt(target);
}
}
I would really appreciate an answer even though I recognize I'm a total beginner with scripts.
Same problem, I renamed it and I got a problem! the name is UpdatePos.
Here is my script, its from the scripting wiki btw:
using UnityEngine;
using System.Collections;
public class ExampleClass : $$anonymous$$onoBehaviour
{
void Start()
{
transform.position = new Vector3(0, 0, 0);
print(transform.position.x);
}
}
Change it to this:
using UnityEngine;
using System.Collections;
public class UpdatePos : $$anonymous$$onoBehaviour
{
void Start()
{
transform.position = new Vector3(0, 0, 0);
print(transform.position.x);
}
}
All I did was change the class name from ExampleClass to UpdatePos.
Answer by nightbane30 · May 08, 2015 at 11:10 PM
Make sure that the name of your script is also CameraLookAt. The class name and file name have to be the same in order for it to work.
It worked! Thank you very much =D The problem now seem to be the object the script is applied to points more like -90° wrong ins$$anonymous$$d of directly towards the target. I tried to offset the rotation +90° in the opposite direction. Hoping it'll help, but it did no good. Yet it seem to move correctly in correspondence with the animation.
Any idea, how I can make it to actually point at the target?
I wasn't sure how to correct the file name in unity itself but found that you can rename the file in the file directory "explorer" and it works.
Answer by german_g · Jan 17, 2018 at 08:33 PM
Just wanted to add an explanation to that great answer above: Usually that problem arises when you create a script file and then rename it. Each time you create a script file, Unity creates a public class with the name you initially give to that script file. So when you rename the script file, it becomes disconnected from that public class. To do a quick check, select the script file in Unity's project window and look at the inspector window to compare script file and class names. In the example below script file name must be PlayerMove
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMove : MonoBehaviour {
}
Quick question, so dose this mean that their can't be spaces in the script files name?
Yes, that means that scripts cannot have space unless you thinker some sorts of converter in Unity that automatically remove the space whenever it request a class that includes spaces.
To be honest, that's more of a waste of memory for the editor (since it got to check it out every single time it check for the class). All you got to do is think = Space. So, as an example, Env Light $$anonymous$$anager.cs would be *Env_Light$$anonymous$$anager.cs* ins$$anonymous$$d if you really need some kind of spaces in the nomenclature of your project's workflow.
Answer by ryanm007 · Sep 20, 2021 at 01:34 AM
For me, the problem was that there was an error in another script.
Once I fixed the error, I could FINALLY attach my other scripts onto my gameobjects.
Answer by Psyminator · Sep 14, 2016 at 07:26 AM
Thanks a lot =D
I'm really grateful for you answers guys!
Your answer
