- Home /
Other
The name 'transform' does not exist in the current context (Fixed, was my fault)
EDIT: I accidentaly created a new public class named MonoBehaviour so this was my own fault and it is fixed now. Thanks to everyone who tried to help.
Can someone please explain to me why this code is giving me the error "CS0103: The name 'transform' does not exist in the current context' in Visual Studio?
using UnityEngine;
using System.Collections;
public class test : MonoBehaviour {
// Use this for initialization
void Start () {
transform.position = Vector3.zero;
}
// Update is called once per frame
void Update () {
}
}
I created this class as a test but I have multiple other classes that give me the same error in Visual Studio and in Unity as well. Some months ago the project worked perfectly fine.
EDIT: After restarting, the error now doesn't show up in Unity anymore and I can add the affected scripts to a GameObject again but the error is still present in VisualStudio.
EDIT 2: It reappeared in Unity after I changed the name of the test class and it's file. But it only appeared for the test class.
Interesting. Do you have other classes that don't have this problem? Does the class name match the file name? Try rena$$anonymous$$g both to something other than Test. Close everything and back everything up, then delete the Library folder and re-open.
Rena$$anonymous$$g the file and class (matching names (as before)) didn't work and deleting the Library folder and re-opening it also didn't work. But now it warns me of inconsistent line endings and that they can produce incorrect compile errors, so I will fix that now and see if this was the problem.
EDIT: Fixing the line endings didn't help.
Try recreating the script. Unity has this weird problem where it won't recognize certain things.
Answer by JackTenSeven · Jul 08, 2016 at 04:40 PM
Could be that the script isn't assigned to a game object and so there is no parent to the script (transform). To fix it you could add the script as a component to any games object in unity and then that game object would be the transform for that script.
I don't think that's the problem as I'm not even able to assign it to a GameObject. It gives me a message saying "Please fix compile errors before creating new script components." I think transform is just a member of $$anonymous$$onoBehaviour so it shouldn't have any issues if it is not assigned to a GameObject.
Answer by tanoshimi · Jul 08, 2016 at 07:18 PM
And what if you use the fully-qualified version?
GetComponent<Transform>.position = Vector3.zero;
That gives me pretty much the same error just with GetComponent ins$$anonymous$$d of transform. Also gameObject is unknown and yields the same error.
Answer by Droidenkiller · Jul 08, 2016 at 09:53 PM
I found the error.
I wanted to do some unit testing but it threw an Exception so I tried this solution:
It kept giving me an exception so I removed the preprocessor directives and then this happened.
Thanks to everyone for trying to help.
If anyone has a solution to the problem in the link, I'd be interested in your solution.