Anend
March 27, 2022, 9:03am
1
Ошибка 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);
}
}
Anend:
Please help
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
Anend
March 27, 2022, 11:48am
3
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.
Anend
March 27, 2022, 12:20pm
6
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.
Anend
March 27, 2022, 12:22pm
8
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);
}
}
Anend
March 27, 2022, 12:23pm
9
passed, I fixed it after 1 answer
Anend:
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);
}
}
As mentioned previously, which you seem to have ignored.
Anend
March 27, 2022, 12:24pm
11
Vector3 mousePos = Camera.Main.ScreenToworldPoint(Input.mousePosition);
Anend
March 27, 2022, 12:25pm
12
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
Anend
March 27, 2022, 12:29pm
16
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.