Unity error CS0246: The type or namespace name 'transform' could not be found

I have copied a c# code from the internet for the camera movement for my FPS game but i get this error: Assets\MouseLook.cs(9,12): error CS0246: The type or namespace name ‘transform’ could not be found (are you missing a using directive or an assembly reference?) Can someone help me out?

This my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MouseLook : MonoBehaviour
{

public float mouseSensitivity = 100f;
public transform playerBody;
float xRotation = 0f;

// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}

// Update is called once per frame
void Update()
{
float mouseX = Input.GetAxis(“MouseX”) * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis(“MouseY”) * mouseSensitivity * Time.deltaTime;

xRotation -= MouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);

transform.localRotation = Quanternion.Euler(xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
}
}

It’s:

Transform

Not:

transform

1 Like

This tells you exactly where the issue is: It’s in MouseLook.cs, line 9, around column 12.

It also tells you what the issue is, though in a backwards kind of manner - it tells you how the compiler is confused, not what caused the confusion (because if it knew, it wouldn’t be confused). In this case, it doesn’t recognise the term “transform”. That’s because you have written it incorrectly. It needs a capital ‘T’.

1 Like

if i type it with capital T i get more errors.

This is the error what i get know after changing the t to T

error CS0103: The name ‘MouseY’ does not exist in the current context and

error CS0103: The name ‘Quanternion’ does not exist in the current context

Apply what I said in my last post to those error messages. Where are they? What is the compiler confused about? What do you need to change in your code to un-confuse it?

You said this came from a tutorial. Make sure you’re following their instructions exactly. Every letter, upper / lower case, space and punctuation mark needs to be identical. Once you know the syntax you can relax a bit, but until then… identical.

P.S. I’m not being a jerk by not telling you what to fix. Error messages are a normal part of programming, learning to read them and how to interpret the sometimes-cryptic descriptions is a key part of it. It gets easier, but you’ve got to put the practice in.

This forum is not for us to fix your typos. You must type everything 100% accurately first. That’s on you.

Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

How to do tutorials properly, two (2) simple steps to success:

Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That’s how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.
Fortunately this is the easiest part to get right: Be a robot. Don’t make any mistakes.
BE PERFECT IN EVERYTHING YOU DO HERE!!

If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there’s an error, you will NEVER be the first guy to find it.

Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

Finally, when you have errors…

The complete error message contains everything you need to know to fix the error yourself.

The important parts of the error message are:

  • the description of the error itself (google this; you are NEVER the first one!)
  • the file it occurred in (critical!)
  • the line number and character position (the two numbers in parentheses)
  • also possibly useful is the stack trace (all the lines of text in the lower console window)

Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don’t have to stop your progress and fiddle around with the forum.

Remember: NOBODY here memorizes error codes. That’s not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

I have a code where I do the movement of a character (specifically a tiger) somewhere I get this error CS0246: Could not find type or namespace name ‘AnimationsTigerMAMA’ (missing a usage directive or assembly reference ?) I already reviewed it but I really don’t understand it, can someone help me

This my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Threading.Tasks;

public class PlayerMovementTigreMAMA : MonoBehaviour
{
[SerializeField] private Rigidbody _rb;

[Header(“Movement values”)]
public float _movementSpeed;
[SerializeField] private float horizontalMovement;
[SerializeField] private float verticalMovement;
[SerializeField] private float rotationSpeed;
float startSpeed;
float runSpeed;
bool isRotating;
[SerializeField] bool isAnotherPlayer;

[Header(“Animations”)]
[SerializeField] private Animator animator;

[SerializeField] LayerMask groundLayer;

[SerializeField] float gravity;
[SerializeField] float turnSmoothVelocity;
[SerializeField] float _smoothRotation;

AnimationsTigerMAMA animationsPlayer;

public Transform cam;

float targetAngle;
float angle;

public Vector3 direction;

private void Start()
{
animationsPlayer = FindObjectOfType();
startSpeed = _movementSpeed;
runSpeed = startSpeed * 1.3f;
}

private void Update()
{
if (Input.GetKey(KeyCode.T))
{
direction += new Vector3(0, 0, 1);
}
if (Input.GetKeyUp(KeyCode.T))
{
direction -= new Vector3(0, 0, 1);
}

if (Input.GetKey(KeyCode.G))
{
direction += new Vector3(0, 0, -1);
}
if (Input.GetKeyUp(KeyCode.G))
{
direction += new Vector3(0, 0, 1);
}

if (Input.GetKey(KeyCode.F))
{
direction += new Vector3(-1, 0, 0);
}
if (Input.GetKeyUp(KeyCode.F))
{
direction += new Vector3(1, 0, 0);
}

if (Input.GetKey(KeyCode.H))
{
direction += new Vector3(1, 0, 0);
}
if (Input.GetKeyUp(KeyCode.H))
{
direction -= new Vector3(1, 0, 0);
}

direction = new Vector3(Mathf.Clamp(direction.x, -1, 1), 0, Mathf.Clamp(direction.z, -1, 1));

Vector3 newDirection = direction;

targetAngle = Mathf.Atan2(newDirection.x, newDirection.z) * Mathf.Deg2Rad + cam.eulerAngles.y;
angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, _smoothRotation);

if (direction.x != 0 || direction.z != 0)
Move(newDirection);
else if (direction.x == 0 || direction.z != 0)
{
if (!animationsPlayer.isPlayingAnimation && animationsPlayer.isAWalkingAnimation == false)
{
animator.Play(“Idle_A”);
}
}
newDirection.y += gravity * Time.deltaTime;
}

Vector3 GetAlignment()
{
RaycastHit hit;
RaycastHit hit2;
RaycastHit hit3;
RaycastHit hit4;

if (Physics.Raycast(transform.position + new Vector3(0.04f, 0.2f, 0.07f), -transform.up, out hit, 0.7f, groundLayer) ||
Physics.Raycast(transform.position + new Vector3(0.04f, 0.2f, -0.12f), -transform.up, out hit2, 0.7f, groundLayer))
{
Physics.Raycast(transform.position + new Vector3(0.04f, 0.2f, 0.07f), -transform.up, out hit, 0.7f, groundLayer);
Physics.Raycast(transform.position + new Vector3(0.04f, 0.2f, -0.12f), -transform.up, out hit2, 0.7f, groundLayer);
Physics.Raycast(transform.position + new Vector3(-0.04f, 0.2f, 0.07f), -transform.up, out hit3, 0.7f, groundLayer);
Physics.Raycast(transform.position + new Vector3(-0.04f, 0.2f, -0.12f), -transform.up, out hit4, 0.7f, groundLayer);

Vector3 newUp = (hit.normal + hit2.normal + hit3.normal + hit4.normal).normalized;

newUp -= new Vector3(0f, 0.7f, 0f);

return newUp;
}

return Vector3.zero;
}

private void Move(Vector3 direction)
{
if (animationsPlayer.isAWalkingAnimation == false)
animator.Play(“Run”);

_movementSpeed = runSpeed;

var matrix = Matrix4x4.Rotate(Quaternion.Euler(0, targetAngle, 0));
var newInput = matrix.MultiplyPoint3x4(direction);

transform.position += newInput * _movementSpeed * Time.deltaTime;

var newInputRotation = matrix.MultiplyPoint3x4(direction);

if (newInputRotation.z >= 0.1f || newInputRotation.x >= 0.1f || newInputRotation.x <= -0.1 || newInputRotation.z <= -0.1)
{
float targetAngle2 = Mathf.Atan2(newInputRotation.x, newInputRotation.z) * Mathf.Rad2Deg;

float angle2 = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle2, ref turnSmoothVelocity, _smoothRotation);

RaycastHit info;

if (Physics.Raycast(transform.position + new Vector3(0.04f, 0.2f, 0.07f), -transform.up, out info, 0.4f, groundLayer))
{
transform.rotation = Quaternion.Lerp(transform.rotation, QuaternionLookRotation(transform.forward, GetAlignment()), 10 * Time.deltaTime);
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(0, angle2, 00), 10 * Time.deltaTime);
}
else
{
transform.up = Vector3.up;
}
}

}

private void OnDrawGizmos()
{
Debug.DrawRay(transform.position + new Vector3(0.04f, 0.2f, 0.07f), -transform.up * 1f, Color.red);
Debug.DrawRay(transform.position + new Vector3(0.04f, 0.2f, -0.12f), -transform.up * 1f, Color.red);
Debug.DrawRay(transform.position + new Vector3(-0.04f, 0.2f, 0.07f), -transform.up * 1f, Color.red);
Debug.DrawRay(transform.position + new Vector3(-0.04f, 0.2f, -0.12f), -transform.up * 1f, Color.red);
}

Quaternion QuaternionLookRotation(Vector3 approximateForward, Vector3 exactUp)
{
Quaternion zToUp = Quaternion.LookRotation(exactUp, -approximateForward);
Quaternion yToz = Quaternion.Euler(90, 0, 0);
return zToUp * yToz;
}
}

I suggest you go back to wherever you got this crazy complicated code and start checking over your work. You clearly must have made typing mistakes or you failed to get all the parts you need. You can fix your own typing mistakes. Here’s how:

Remember: NOBODY here memorizes error codes. That’s not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

The complete error message contains everything you need to know to fix the error yourself.

The important parts of the error message are:

  • the description of the error itself (google this; you are NEVER the first one!)
  • the file it occurred in (critical!)
  • the line number and character position (the two numbers in parentheses)
  • also possibly useful is the stack trace (all the lines of text in the lower console window)

Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly? Are you structuring the syntax correctly? Look for examples!

All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don’t have to stop your progress and fiddle around with the forum.

Thanks, I fixed it, the name does not exist within the name I was calling it, i change it and it’s working.

Next time, start your own thread.

1 Like