hi!
i’m relatively new to unity and programming in genral, and i get this error for no visible reason
Heres the script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class movement_and_shooting_Lvl4 : MonoBehaviour
{
public Rigidbody2D rb;
Vector2 e = new Vector2 (8, 0);
public GameObject bullet;
public GameObject shooterplace;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(Input.GetKey(“d”)){
rb.AddForce(e);
}
if(Input.GetKey(“a”)){
rb.AddForce(-e);
}
if(Input.GetKey(“right”)){
rb.AddForce(e);
}
if(Input.GetKey(“left”)){
rb.AddForce(-e);
}
if(Input.GetButtonDown(“Fire1”)){
shoot();
}
Shoot()
{
Instantiate(bullet, shooterplace.position, Quaternion.identity);
}
}
}
Please post your code using code tags, makes it much easier for someone to help you out.
2 Things, (1) add void to your Shoot() Function, so it would be void Shoot(), (2) You are calling shoot(), but your function is named Shoot(), C# is case sensitive.
What do you mean “no visible reason”? The error tells you the line/column number and that it’s expecting a semi-colon.
You’ve defined a “Shoot” function like this “Shoot ()” which isn’t correct. You didn’t specify the return type so the compiler has no idea what you’re trying to do. Also, you’re calling this function with a lowercase “shoot”. Note that the language is case-sensitive.
I’ll move your post to the scripting forum btw because your post is nothing to do with 2D features specifically.
“This error?” You don’t need to tell us. That’s for you! Here’s why:
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.
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.