Error cs1002; expected

Ошибка cs1002;
Please help, here is the script:

using UnityEngine;

public class NewBehaviourScript : MonoBehaviour{
public Transform player;
[SerializeField]
private float speed = 10f;

void OnMouseDrag () {
Vector3 mousePos = Main Camera.ScreenToworldPoint (Input.mousePosition);
mousePos.x = mousePos.x > 2.5f ? 2.5f: mousePos.x;
mousePos.x = mousePos.x < -2.5f ? -2.5f: mousePos.x;
player.position = Vector2.MoveTowards (player.position, new Vector2 (mousePos.x, player.position.y), speed * Time.deltaTime);

}
}

Please read: https://discussions.unity.com/t/481379

Note that the error tells you the line the error is on (so worth passing that info on) so all you have to do is look at the line and see if you’ve made any typos which you have:

Missing period/dot “.” after “Main” (even though the code itself is wrong too)

Vector3 mousePos = Main Camera.ScreenToworldPoint (Input.mousePosition);

Also, that tag list :eyes:

Assets\Scripts\NewBehaviourScript.cs(9,40): error CS1061: ‘Camera’ does not contain a definition for ‘ScreenToworldPoint’ and no accessible extension method ‘ScreenToworldPoint’ accepting a first argument of type ‘Camera’ could be found (are you missing a using directive or an assembly reference?)

It’s not Main.Camera it’s Camera.main

For your second response, capitalization is very important.

As above but also you cannot be “sort of correct” when programming; you need to be 100% correct.

Given what you’ve posted I would recommend you do a several things assuming you’ve not already:

  • Use the API docs to figure out the correct syntax (search for stuff): https://docs.unity3d.com/ScriptReference/

  • Ensure you’ve got the correct IDE Unity package installed to help you with your IDE (Rider, Visual Studio etc). You’ll get syntax help/selection then.

  • Follow some begineer scripting tutorials such as here

If not, it’s going to be a painful process to figure out typos or basic C# language features using the forums.

I fixed it a long time ago but it still didn’t help.

Fixed what? I replied to you within the same minute you sent the message.

using UnityEngine;

public class NewBehaviourScript : MonoBehaviour{
public Transform player;
[SerializeField]
private float speed = 10f;

void OnMouseDrag () {
Vector3 mousePos = Camera.main.ScreenToworldPoint (Input.mousePosition);
mousePos.x = mousePos.x > 2.5f ? 2.5f: mousePos.x;
mousePos.x = mousePos.x < -2.5f ? -2.5f: mousePos.x;
player.position = Vector2.MoveTowards (player.position, new Vector2 (mousePos.x, player.position.y), speed * Time.deltaTime);

}
}

passed, I fixed it after 1 answer

As mentioned previously, which you seem to have ignored.

Vector3 mousePos = Camera.Main.ScreenToworldPoint(Input.mousePosition);

if I write “main” with a capital letter, then another error pops up

Why are you trying to write main with a capital letter? The link I sent leads here:
https://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html

Please stop using the forums like this. Unfortunately, you are not reading what has been posted. Did you look at the API docs? Unfortunately not. Did you use code-tags as requested? Again, unfortunately not.

2 Likes

I write with a small letter as you threw it, this error is returned

There must be a language barrier here because you are NOT reading the docs or what is being said.

The docs say “ScreenToWorldPoint”. You wrote “ScreenToworldPoint” and think it’s the same.

It’s also “Camera.main”.

Take the time to read the above, so far you’ve ignored me. Note that you’re replying in another language and the forum is stopping your posts because of it.

Так что извиняйте

Wow, it worked, thanks