So this is my script and i get 1 CS1003 error and dont know what it means:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Jump : MonoBehaviour;
{
// Start is called before the first frame update
void Start()
{
bool jump = false;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
if (jump == false)
{
Rigidbody.AddForce(transform.up * 200);
jump = true;
WaitForSeconds(0x5);
jump = false;
}
}
}
}
Hi and welcome.
It would help if you posted the error message, but iâd assume that the problem is that you are trying to use WaitForSeconds on the main thread. Also, while your code is properly formatted, we have code tags you can use to add syntax highlighting. Itâs the first sticky on this subforum.
Copy and paste the actual error. We donât have error codes memorized, plus the error will include more information like line numbers that will help us narrow it down quickly and easily.
But yeah, WaitForSeconds doesnât work like that. It has to be in a coroutine.
Thanks but would you mind explaining what is a main thread and how to avoid this error?
The error message is Assets/Jump.cs(5.34): error CS1003: syntax error . expected.
Oh, there shouldnât be a semicolon after MonoBehaviour.
And when we say copy and paste, we literally mean copy and paste. You can select the error and just ctrl-C to copy. Donât try and retype it, as that will introduce random little mistakes. Like, you typed a period in the quotes, when (looking at your code) it should be a semicolon.
Youâve declared âjumpâ inside of Start. This should be at class level, not inside the method âStartâ, if you expect to access it elsewhere in the class. Like here:
if (jump == false)
âjumpâ is not in scope here.
Rigidbody.AddForce(transform.up * 200);
Rigidbody is a class name, AddForce is an instance method⌠WHICH Rigidbody to you expect to be accessing? You need a reference to it⌠maybe âthis.GetComponent()â if you expect the Rigidbody attached to the same GameObject as this script.
WaitForSeconds(0x5);
WaitForSeconds is a class for a coroutine yield instruction. You usually say âyield return new WaitForSeconds(5f);â if you expect to wait 5 seconds.
Furthermore this is being done in Update, and not a Coroutine, so itâs not going to work correctly.
And why are you using the hex prefix? I mean 5 and 0x5 are technically the same since the value is < 16. But Like say you said 0x10, thatâs not the same as 10.
And lastly, youâre also setting unscope 'jumpâs again before and after this WaitForSeconds.
âŚ
As for the CS1003 error youâre getting. It honestly has nothing to do with anything at this point. Your code has so many syntax errors that the compiler is likely just confused by the ; after the MonoBehaviour that it canât make heads or tails and is just throwing an expectation errorâŚ
Likely itâs saying â,â (comma) since if you follow a inheritance declaration with anything, you generally separate them with commas. Like so:
public class SomeClass : SomeParentClass, IDisposable, IEnumerable
this is me generically implementing multiple interfaces.
But thatâs not your problem⌠you just inadvertently put a â;â in there. As well as other mistakes.
You seem to be new, so dont worry if this goes a bit over your head for now. A thread is a basically a process, but not quite. The cpu can run one thread per logical cpu core. A programm by default always only runs on one thread, thus called the main thread. On that thread, all the code in your programm is executed sequentially.
However, one programm can make use of multiple threads and thus multiple cpu cores, in order to parallelize the wordload. But thatâs where programming gets complicated, and you dont really have to worry about it now.
A Coroutine is similar, but it does not parallelize work, instead it makes use of concurrency by running a second workload on the same thread. For example, if you want to execute something and then wait 5 seconds and then execute something again and so on, then you could use a Coroutine for that. In a Coroutine you can then use âWaitForSecondsâ to make it wait. While the Coroutine is waiting, the other workload (the ânormalâ main thread code) will be executed, until the Coroutine wakes up again and so on.
I just thought iâd explain it, but again, you dont really have to deal with this topic until way later, potentially never depending on the kind of programs you write. It is not related to your problem, as you dont have to create another thread or a coroutine, or even call WaitForSeconds, in order to make something jump.
As for your actual problem i suggest you watch some tutorials on basic movement and jumping, to get an idea for how itâs done. There are plenty of such tutorials, iâll just link one;
sometimes some errors only appear after fixing others, since the compiler doesnt bother to compile the entire program if it cant make sense of the start already.
When you fix one, others will show up. Right now the compiler basically just sees the first one and then canât make sense of the file at all, and so doesnât know that theyâre issues.
When your syntax errors are in the realm where the compiler canât make sense of anything following it, thereâs not a whole lot it can do. You ended a class declaration with a â;â, it doesnât know what anything following that would be. It doesnât know you continue on declaring a class.
Itâd be like if I just randomly started speaking boom tangle nut bag squeezy nuggets with a chirp chirp fully norte gimp bugket, chip, whoseit?!..
âŚ
Sure, I have spelling mistakes in there, and grammatical misuse of commas and questions marks. But I mean⌠how can you tell me that? What if I meant to say ânorteâ and âbugketâ? Itâs all nonsense anyways!
This does nothing. Debug.Log is not a returntype. Log() is a function of the Debug type. You also tried to name your variable input.AxisRawHorizontal, which is first of all not a valid name, and secondly i believe you didnt even intend to use it as a name but instead ⌠print its value or something along those lines.
You then removed the body of your update function by adding a semicolon and removing the {}:
// Update is called once per frame
void Update();
This means, âUpdate()â literally executes no code. Iâm not even sure if this compiles for non abstract methods either.
Which i assume is supposed to be a method declaration, but then again, Debug.Log is not a type, and GetInput.AxisRawHorizontal is not a valid name, not to mention that the rest of the method declaration is also incorrect.
Coming back to my first statement, iâd really recommend you to start learning coding at the very bottom. There are a few good tutorials to help you. I would recommend you looking into the youtube tutorial series on C# + Unity by Sebastian Lague.
Not really. Brackeys and a couple others are rather popular, but mostly for beginner topics imho. I personally like Sebastian Lague since he cuts into a lot of really interresting topics and breaks them down in an interrestingly educational and entertaining way. For getting started i dont think you need more than his series on gamedevelopment. If you ever feel like you need to learn more about a topic, google that specifically and learn about it. After you went through that series, or any other, youâll need to properly start âlearning by doingâ anyways, where you set yourself incremental goals you try to solve / develop, and as such improve your skills. One advantage of the series i recommended is that he already offers little projects (fit for your skill level according to that tutorial series) while teaching you.
Can you guys help me I am getting error code 1003; expected my code is:
using UnityEngine;
public class playermovment : MonoBehaviour;
{
public Rigidbody rb;
public float forwardForce = 2000f;
public float sidewaysForce = 500f;
// Update is called once per frame
void FixedUpdate()
{
rb.AddForce(0, 0, 500 * Time.deltaTime);