I followed the tutorial, checked several times. I changed private to public, same error. Deleted the word “private” resulting in 20+ errors.
I have 2 errors.
Line 39 ‘private’ not valid for this item(CS0106)
Line 48 ‘private’ not valid for this item(CS0106)
39: private IEnumerator PlayerMoveTowards(Vector3 target);
{ while (Vector3.Distance(transform.position, taret) > 0.1f)
Vector3 destination = Vector3.MoveTowards(transform.position, target, playerspeed * Time.deltaTime);
transform.position = destination;
yield return null;
}
48: private void OnDrawGizmos();
{ Gizmos.color = color.red;
I am new to unity & C#
Gizmos.DrawSphere(targetPosition, 1);
Please add your code inside a code block using:
``` csharp
//your code here
```
and add your whole script not just the lines that you believe the error is.
That being said: In a tutorial you should check to make sure that everything is absolutely correct and the same as the tutorial, that is capitalization, semicolons and so on. Check your lines again to make sure you have copied them exactly as shown, the error is pretty obvious.
1 Like
As Meredoth notes, you’re just making typing mistakes.
You can fix your own typing mistakes. Here’s how:
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.
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.
Thanks - i did check all the capitalization and spelling a few times. I am starting to think the tutorial had other codes in the background i couldn’t see, since i noted lines changed positions half way through the tutorial.
The errors are now showing on lines 50 & 70 - same reason.
using System.Collections;
using UnityEngine;
using UnityEngine.InputSystem;
//[RequireComponenet(typeof(CharacterController))]
[RequireComponenet(typeof(Rigidbody))]
public class ClickToMove : MonoBehaviour
{
[SerializeField]
private InputAction MouseClickAction;
[SerializeField]
private float playerSpeed = 10f;
[SerializeField]
private float rotationSpeed = 3f;
private Camera mainCamera;
private Coroutine coroutine;
private Vector3 targetPosition;
private CharacterController characterController;
private Rigidbody rb;
private int groundLayer;
private void Awake()
{
mainCamera = mainCamera.main;
characterController = GeComponent<CharacterController>();
rb = GetComponent<Rigidbody>();
groundLayer = LayerMask.NameToLayer("Ground");
}
private void OnEnable()
{
MouseClickAction.Enable();
MouseClickAction.performed += Move;
}
private void OnDisable()
{
MouseClickAction.performed -= Move;
MouseClickAction.disable();
}
private void Move(InputAction.CallbackContext context)
{
Ray ray = mainCamera.ScreenPointToRay(Mouse.current.position.ReadValue());
if (Physics.Raycast(ray: ray, hitInfo: out RaycastHit hit) && hit.collider && hit.collider.gameObject.layer.CompareTo(groundLayer) == 0) {
if (coroutine != null) StopCoroutine(coroutine);
coroutine = StartCoroutine(PlayerMoveTowards(hit.point));
targetPosition = hit.point; }
private IEnumerator PlayerMoveTowards(Vector3 target)
{ float playerDistanceToFloor = transform.position.y - target.y;
target.y += playerDistanceToFloor;
while (Vector3.Distance(transform.position, taret) > 0.1f)
// Ignores Collisions
Vector3 destination = Vector3.MoveTowards(transform.position, target, playerspeed * Time.deltaTime);
// transform.position = destination;
// Character Controller
Vector3 direction = target - transform.position;
Vector3 movement = direction.normalized * playerSpeed * Time.deltaTime;
//characterController.Move(movement);
//Rigidbody
rb.velocity = direction.normalized * playerSpeed;
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction.normalized), rotationSpeed * Time.deltaTime);
yield return null;
private void OnDrawGizmos()
{
Gizmos.color = color.red;
Gizmos.DrawSphere(targetPosition, 1);
}
}
}
}
Your PlayerMoveTowards and OnDrawGizmos methods are inside the scope of the Move method. They need to be moved outside.
This is different from what you had written before: there are no semicolons at the end of your lines here.
Are you sure this is how it is written in the tutorial? This is wrong, this shouldn’t compile. It seems very strange for a tutorial to have so obvious errors.
This is what is confusing me - on the first half of the tutorial, there were no semicolons, on the 2nd half they were there. I added and removed them but it doesn’t change the error.
I just moved the Gizmoes line outside of movement and I get a new error
error CS8803 : Top-level statements must precede namespace and type declarations
If this is how the tutorial is written then it is a horrible tutorial. There is no point as a beginner to try to fix it yourself or ask for every error in the forum as without any doubt it will have many more errors after that. These are some serious basic level errors.
I would advise to stop this tutorial and do another one, you are not in a level right now to be able to fix the errors a tutorial has, even with help from the forum.
1 Like
THanks - i agree with you. I have been at it for months and this is the best tutorial i have found to date for trying to do a simple mouse-click-to-move 3d game. With such severely limited tutorials, I am at a loss of where to go next.
is there a site other than youtube, google, here, MS, that would have a decent tutorial?
yea, thats where i started. There are no tutorials on mouse click movent.
i am starting to wonder if it is something so basic there are no tutorials on it, that would make sense why it is so hard to find.
Programming is about solving problems. Tutorials teach you how to code and use Unity.
If by tutorial, you mean something that will solve your problem for you, then I’m afraid that this will not do you any good and I am firmly against doing that.
Currently at your level, you shouldn’t look for specific things on how to do something, but concentrate on learning the absolute basics, like basic syntax, basic scripting and so on. If you learn those, you could make what you want and you would be in a position to solve all those errors by yourself.
Start learning with Unity learn and leave for now the idea about implementing specific behaviors, when you don’t even know the basic syntax of C#. Learning to code takes time and patience.
1 Like
thats fair. I have been able to develop landscapes and terrain. My goal was to develop a non-combat educational game for members of the cherokee tribe. I was thinking all i had to do was learn how to script a few things like movement and NPC interaction… now it feels like i have to learn how to code diablo 4 before i can get to the basics, but if that is how unity is designed, it looks like i will have to take a few college courses on the more advanced stuff first. I have developed a lot of software over the years, but coming back to it I was just shocked that event the basic how-to functions are not discussed anywhere. Thanks for the help.
Its sort of ironic, i can get the 3d WASD type of FPS game developed just fine, but the simpler the design it seams the harder it is to make. just switching from WASD to mouse click is a nightmare.
It sounds like perhaps you moved to fast ahead?? It’s a bit like “I made a go-cart, that was easy, but now I tried to make an airplane, which is even simpler and lighter, and it’s a nightmare!”
You need to build your knowledge upon previous layers of solid foundational knowledge.
Like this guy does:
Imphenzia: How Did I Learn To Make Games:
Two steps to tutorials and / or example code:
- do them perfectly, to the letter (zero typos, including punctuation and capitalization)
- stop and understand each step to understand what is going on.
If you go past anything that you don’t understand, then you’re just mimicking what you saw without actually learning, essentially wasting your own time. It’s only two steps. Don’t skip either step.
1 Like
Hello
I don’t know if I’ll be any help, but I have one tip concerning your code above. When you get an error that you don’t understand, it may well be about you forgetting to close a bracket (or closing one too many).
" error CS8803 : Top-level statements must precede namespace and type declarations" probably means you didn’t put this “}” at the end of the script.
Just for future reference.
1 Like
You moved “the line” outside? Are you even aware of what code belongs together? OnDrawGizmos is a method. So the following code block (the curly braces following) belong to that method. Also when you get this error, it seems you have moved it outside the whole class which is also wrong^^. Methods need to be inside a class. That’s why proper indentation helps to “see” where a block begins and ends.
Referring to your code you posted in your first reply (post #4) the body of your class starts with the curly brace in line 8 and ends in line 79. So all methods have to be inside those curly braces. Outside / after that closing curly brace you’re no longer inside the class and here you can only declare new types.
I just took a closer look at your code and you have countless spelling errors, missing captialisation, additional character or missing characters. Computers / compilers are not humans and programming languages are exact languages. There is very little room for interpretation besides whitespace.
Your OnDrawGimos method also was declared inside your PlayerMoveTowards
coroutine and the PlayerMoveTowards
coroutine was also declared inside your Move
method. All those methods need to be inside the class and not inside each other.
I tried to fix as much as I can see, but this is a “one time service”. Don’t just come here, drop your script and expect it being fixed. As it was said, tutorials are meant to teach you certain concepts with the aid of an example. The goal is that you learn and understand how it works. My guess is that you’re lacking much more basics about C# and you probably should start somewhere lower.
using System.Collections;
using UnityEngine;
using UnityEngine.InputSystem;
//[RequireComponent(typeof(CharacterController))]
[RequireComponent(typeof(Rigidbody))]
public class ClickToMove : MonoBehaviour
{
[SerializeField]
private InputAction MouseClickAction;
[SerializeField]
private float playerSpeed = 10f;
[SerializeField]
private float rotationSpeed = 3f;
private Camera mainCamera;
private Coroutine coroutine;
private Vector3 targetPosition;
private CharacterController characterController;
private Rigidbody rb;
private int groundLayer;
private void Awake()
{
mainCamera = Camera.main;
characterController = GetComponent<CharacterController>();
rb = GetComponent<Rigidbody>();
groundLayer = LayerMask.NameToLayer("Ground");
}
private void OnEnable()
{
MouseClickAction.Enable();
MouseClickAction.performed += Move;
}
private void OnDisable()
{
MouseClickAction.performed -= Move;
MouseClickAction.Disable();
}
private void Move(InputAction.CallbackContext context)
{
Ray ray = mainCamera.ScreenPointToRay(Mouse.current.position.ReadValue());
if (Physics.Raycast(ray: ray, hitInfo: out RaycastHit hit) && hit.collider && hit.collider.gameObject.layer.CompareTo(groundLayer) == 0)
{
if (coroutine != null) StopCoroutine(coroutine);
coroutine = StartCoroutine(PlayerMoveTowards(hit.point));
targetPosition = hit.point;
}
}
private IEnumerator PlayerMoveTowards(Vector3 target)
{
float playerDistanceToFloor = transform.position.y - target.y;
target.y += playerDistanceToFloor;
while (Vector3.Distance(transform.position, target) > 0.1f)
{
// Ignores Collisions
Vector3 destination = Vector3.MoveTowards(transform.position, target, playerSpeed * Time.deltaTime);
// transform.position = destination;
// Character Controller
Vector3 direction = target - transform.position;
Vector3 movement = direction.normalized * playerSpeed * Time.deltaTime;
//characterController.Move(movement);
//Rigidbody
rb.velocity = direction.normalized * playerSpeed;
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction.normalized), rotationSpeed * Time.deltaTime);
yield return null;
}
}
private void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawSphere(targetPosition, 1);
}
}
ps: Do you have a link to the tutorial? A lot of the errors and mistakes in your code can not be in the tutorial.
Here’s a list of things I found that I can still remember:
- “RequireComponenet” instead of “RequireComponent”
- “mainCamera.main” instead of “Camera.main”
- “MouseClickAction.disable();” instead of “MouseClickAction.Disable();”
- “taret” instead of “target”
- The while loop is missing curly braces to include the rest of the coroutine. Otherwise only the single statement that follows would be “inside” the while.
- “playerspeed” instead of “playerSpeed”
- “color.red” instead of “Color.red”
2 Likes