C# Errors "Nightmare"

Hello,

I am following the basic “Nightmare” tutorial:

http://willgoldstone.com/utd2014.pdf

And this is my code:

	public float speed = 6f;

	Vector3 movement;
	Animator anim;
	Rigidbody playerRigidbody;
	int floorMask;
	float camRayLength = 100f;

	void Awake ()
	{
		floorMask = LayerMask.GetMask("Floor")
		anim = GetComponent <Animator> ();
		playerRigidbody = GetComponent <Rigidbody> ();
	}

	void FixedUpdate ()
	{
		float h = Input.GetAxisRaw("Horizontal");
		float v = Input.GetAxisRaw("Vertical");
	
	    Move(h,v);	
		Turning();
		Animating(h,v);
	{

	void Move (float h, float v)
	{
		movement.Set(h, 0f, v)

		movement = movement.normalized * speed * Time.deltaTime;
		
		playerRigidbody.MovePosition (transform.position + movement);
	}

	void Turning()
	{
		Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition)
		
		RaycastHit floorHit;
		
		if(Physics.Raycast(camRay, out flootHit, camRayLength, floorMask))
				{
					Vector3 playerToMouse = floorHit.point - transform.position;
					playerToMouse.y = 0f;

					Quaternion.newRotation = Quaternion.LookRotation (playerToMouse);
					playerRigidbody.MoveRotation(newRotation);
				{
	
 	}		
	
	void Animating(float h, float v)
		{
			bool walking = h != 0f || v != 0f;
			Animator.SetBool ("IsWalking", walking);
		}
}

And I got this errors:

You really want to spend some time understanding why each symbol exists before you try too much more coding. Also start with a simpler tutorial until you have the basics down.

Welcome to the most common error you'll have while still learning a language. Missing terminators (semi colons and braces in this case) will make you sleep uneasy for the foreseeable future. Just wait till you start getting off-by-one errors.

4 Answers

4

Add symbol ‘;’ for below line of your code:

 floorMask = LayerMask.GetMask("Floor"); //add ";"

And

 movement.Set(h, 0f, v);

And

 Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);

I hope that it will help you.

Yep it fixed the anim errors but not the others :(

Thanks! I will look more careful at the code. Thanks! Now I only egt Parsing error on line 54,1

@Ice-Cold-productions More information on an error is possible.

Parsing error on last line of file normally means unbalanced braces. Count all your { and }. They should match.

@BoredMormon In my code count of characters '{' and '}' matches. Therefore it was interesting, where there an parsing error:)

you didnt closed your fixed update

void FixedUpdate ()
    {
        float h = Input.GetAxisRaw("Horizontal");
        float v = Input.GetAxisRaw("Vertical");
 
        Move(h,v);  
        Turning();
        Animating(h,v);
    }

neither this

if(Physics.Raycast(camRay, out flootHit, camRayLength, floorMask))
                {
                    Vector3 playerToMouse = floorHit.point - transform.position;
                    playerToMouse.y = 0f;
 
                    Quaternion.newRotation = Quaternion.LookRotation (playerToMouse);
                    playerRigidbody.MoveRotation(newRotation);
                }

if you are using monodevelop you can just highlight a } or { or ( or ) and it will show you the corresponding brace, if it doesn’t make sense, you probably have one too many or not enough :stuck_out_tongue:

I usually comment like this

void myFunction()
{

  if(someStuff == someOtherStuff)
  {
     doSomethingAwesome();
  }//  if(someStuff == someOtherStuff)

}//myfunction

this allows for easier reading. and (hopefully) less mistakes :slight_smile:

Be cautious with using comments like this. While some people use it and love it, if you don't keep your comments maintained every time you modify the code then they will become more of a hindrance then a help. And maintaining comments that don't enhance your code becomes very tedious after a while. In the case of {} its normally obvious where they belong, comments add little value. This is mostly a matter of personal preference. But you have been warned.

@BoredMormon: Right, such comments contains redundancy and they can make it actually worse. A lot IDEs like Visual Studio provide a collapse button on each bracket. If you have long and nested blocks it helps a lot to just collapse some blocks to easily see what belongs together.

Mono Develop also lets you collapse. If you can find the check box hidden deep inside one of the options pages. My personal preference is indentation. If I need to collapse because my script is getting to long then its time to make it into two scripts. Its far better to learn to read code and know what the code does, then to use redundant comments. (Note: There are plenty of places where comments are useful. I just don't believe this is one of them).

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
	public float speed = 6f;

	Vector3 movement;
	Animator anim;
	Rigidbody playerRigidbody;
	int floorMask;
	float camRayLength = 100f;

	void Awake ()
	{
		floorMask = LayerMask.GetMask ("Floor");
		anim = GetComponent <Animator> ();
		playerRigidbody = GetComponent <Rigidbody> ();
	}

	void FixedUpdate ()
	{
		float h = Input.GetAxisRaw ("Horizontal");
		float v = Input.GetAxisRaw ("Vertical");
	
		Move (h, v);	
		Turning ();
		Animating (h, v);
	}

	void Move (float h, float v)
	{
		movement.Set(h, 0f, v);

		movement = movement.normalized * speed * Time.deltaTime;
		
		playerRigidbody.MovePosition (transform.position + movement);
	}

	void Turning()
	{
		Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
		
		RaycastHit floorHit;
		
		if (Physics.Raycast (camRay, out flootHit, camRayLength, floorMask)) {
						Vector3 playerToMouse = floorHit.point - transform.position;
						playerToMouse.y = 0f;

						Quaternion.newRotation = Quaternion.LookRotation (playerToMouse);
						playerRigidbody.MoveRotation (newRotation);
				}
	
 	}		
	
		void Animating(float h, float v)
		{
			bool walking = h != 0f || v != 0f;
			Animator.SetBool ("IsWalking", walking);
		}
}

Now I got other errrors. But I get the most of all scpripting, only the codes not

Assets/Scripts/Player/PlayerMovement.cs(45,50): error CS0103: The name `flootHit’ does not exist in the current context

Assets/Scripts/Player/PlayerMovement.cs(45,29): error CS1502: The best overloaded method match for `UnityEngine.Physics.Raycast(UnityEngine.Ray, out UnityEngine.RaycastHit, float, int)’ has some invalid arguments

Assets/Scripts/Player/PlayerMovement.cs(45,29): error CS1503: Argument #2' cannot convert object’ expression to type `UnityEngine.RaycastHit’

Assets/Scripts/Player/PlayerMovement.cs(46,73): error CS0165: Use of unassigned local variable `floorHit’

Assets/Scripts/Player/PlayerMovement.cs(49,60): error CS0117: UnityEngine.Quaternion' does not contain a definition for newRotation’

Assets/Scripts/Player/PlayerMovement.cs(50,79): error CS0103: The name `newRotation’ does not exist in the current context

Assets/Scripts/Player/PlayerMovement.cs(50,65): error CS1502: The best overloaded method match for `UnityEngine.Rigidbody.MoveRotation(UnityEngine.Quaternion)’ has some invalid arguments

Assets/Scripts/Player/PlayerMovement.cs(50,65): error CS1503: Argument #1' cannot convert object’ expression to type `UnityEngine.Quaternion’

Assets/Scripts/Player/PlayerMovement.cs(58,34): error CS0120: An object reference is required to access non-static member `UnityEngine.Animator.SetBool(string, bool)’

It is realy annoying, but I get the codes for the biggest part

First of all, don't post such "updates" as answer. Answers should answer the question. This is not a forum, this is a Q&A site. Next thing is, didn't most here told you to look carefully at your code? You still have spelling mistakes which i've spotted within 2sec. "flootHit" is not the same as "floorHit". There should be no dot in here: "Quaternion.newRotation" "Animator.SetBool(" should be "anim.SetBool("

Okay sorry, I didnt know that.