There are no error in the script, but unity detect compile error...

This problem has been fix by myself, forced to fix because: “we are served only by ourselves”.

Hello here is my script:

using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class BasicEnnemyTargetv2 : MonoBehaviour
{
    public float health = 100f;

    public void TakeDamage(float amount)
    {
        health -= amount;
        if (health <= 0f)
        {
            Die();
        }
    }

    void Die()
    {
        Destroy(gameObject);
    }
}

this is a script for dealing damage against the ennemies

There are no error in this script that i done from a video, only this one is affected.
The name of the class is the same as the file name so no problem about that.
The error:

  Please fix compile errors before creating new script components

This isn’t the first time i meet this message but this is the first time that i’m not able to fix this.

I hope some of you can help me.

Have a nice day.

You need by all the way to check if there you variables Capitals letters / smalls letters are the same in your script.

What makes you think this is the script that has the compile errors? Unity should tell you which script has the errors, and which line of the script.

Also, are you really sure the name of the file and the name of the class are the same? You’ve misspelled a word in your class name, so you’ve really misspelled it in both places?

BasicEnnemyTargetv2

Hi, it’s appening the same to me, this is my script:

ublic class PlayerController : MonoBehaviour {

//movement variable
public float maxSpeed;

//jumping variable
bool grounded = false;
float groundCheckRadius =0.2f;
public LayerMask groundLayer;
public Transform groundCheck;
public float jumpHeight;
Rigidbody2D myRB;
Animator myAnim;
bool facingRight;

// Use this for initialization
void Start () {
myRB = GetComponent ();
myAnim = GetComponent ();

facingRight = true;

}

// Update is called once per frame
void Update (){
if(grounded && Input.GetAxis (“Jump”)>0){
grounded =false
myAnim.SetBool(“isGrounded”,grounded);
myRB.AddForce(new Vector2(0,jumpHeight));
}

}

is a script for the movement and the ground check… i think is right but i can’t drag it to my character.
The error message says there’s an “Unexpected symbol myAnim”.

I hope some one can help me too.
bye

For the name of the class, i copy-paste the name of the file to the class,

So i think it’s definitly Unity who produce the problem, unity say me when i select the script that the name of class doesn’t link to the name file.

If Unity says specifically that they don’t match, then you should go and double check.


Imgur

i will try to change the name of the file, and see if that change something

EDIT: I tried to change the name of the file and the class, but definitely,
Unity is the principal problem.

So i need to wait for a update to make this script work again.

i discovered that the error make the drag and drop of files in file explorer buggy and it’s impossible to do this when you get the error

i discovered also that if i don’t delete or move the script, i can’t play the game :

Imgur

Finally i decided to move all script in other file and import them back one by one in Unity and now the compile error is showing with my knife damage script :

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Knife : MonoBehaviour
{
    public float damage = 50f;
    public float range = 5f;
    public float FireRate = 1f;

    public Camera fpsCam;

    private float NextTimetoCut;

    void Update()
    {
        if (Input.GetButton("Fire1") && Time.time >= NextTimetoCut)
        {
            Cut();
            nextTimetoCut = Time.time + 1f / FireRate;
        }
    }

    void Cut()
    {
        RaycastHit hit;

        if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
        {
            Debug.Log(hit.transform.name);
        }
    }
}

It’s was working well before i create my BasicTarget script

bump bump bump up!

my problem still not solved, i think i will give up, and try to create an other game in the 2 next days

Unity is just shit like that sometimes. and Idiots in the comments always think your the stupid one and give you answers you’ve already seen 100 times. This is why Im switching to a different engine

Yeah I hate those idiots :roll_eyes:

3 Likes

It’s now happened to me. This is my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseLook : MonoBehaviour
{
public float mouseSensitivity = 100f;
public Transform playerBody;
float xRotation = 0f;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
float mouseX = Input.GetAxis(“Mouse X”) * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis(“Mouse Y”) * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quanternion.Euler(xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
}
}
It is a script for making the player see around.
A bit of script is right but an error (somehow)
The Bit of script it was is this:
public class MouseLook : MonoBehaviour
This is the bit of script which is supposed to work.
But it’s not.
And also you will never know how I learnt this bit of code in a few days with Unity.

Use code tags in the future, and actually post the error you are getting. The error will include the line number, which points you directly to the problem. But in this case I’m sure the issue is you’re missing a semicolon on the line "xRotation -= mouseY" in your Update method. Your code editor should be pointing out this kind of error as soon as you type it.

I’m having a problem here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public CharacterController cotroller;
public float JumpHeight = 3f;
public float speed = 12f;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
Vector3 velocity;
public float gravity = -9;
bool isGrounded;
}
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
float x = Input.GetAxis(“Horizontal”);
float z = Input.GetAxis(“Vertical”);
Vector3 move = transform.right * x + transform.forward * z;
cotroller.Move(move * speed * Time.deltaTime);
velocity.y += gravity * Time.deltaTime;
cotroller.Move(velocity * Time.deltaTime);
if (Input.GetButtonDown(“Jump”) && isGrounded)
{
velocity.y = Mathf.Sqrt(JumpHeight * -2f * gravity);
}
}

Why would you make a post in a thread without reading the rest of the thread?

Read the post above yours and try again.

1 Like

im having troubles someone help,unity keeps saying ‘please fix compile errors before creating a new scrit component’.

here is my script

using UnityEngine;
using UnityEngine.Events;

public class CharacterController2D : MonoBehaviour
{
[SerializeField] private float m_JumpForce = 400f; // Amount of force added when the player jumps.
[Range(0, 1)] [SerializeField] private float m_CrouchSpeed = .36f; // Amount of maxSpeed applied to crouching movement. 1 = 100%
[Range(0, .3f)] [SerializeField] private float m_MovementSmoothing = .05f; // How much to smooth out the movement
[SerializeField] private bool m_AirControl = false; // Whether or not a player can steer while jumping;
[SerializeField] private LayerMask m_WhatIsGround; // A mask determining what is ground to the character
[SerializeField] private Transform m_GroundCheck; // A position marking where to check if the player is grounded.
[SerializeField] private Transform m_CeilingCheck; // A position marking where to check for ceilings
[SerializeField] private Collider2D m_CrouchDisableCollider; // A collider that will be disabled when crouching

const float k_GroundedRadius = .2f; // Radius of the overlap circle to determine if grounded
private bool m_Grounded; // Whether or not the player is grounded.
const float k_CeilingRadius = .2f; // Radius of the overlap circle to determine if the player can stand up
private Rigidbody2D m_Rigidbody2D;
private bool m_FacingRight = true; // For determining which way the player is currently facing.
private Vector3 m_Velocity = Vector3.zero;

[Header(“Events”)]
[Space]

public UnityEvent OnLandEvent;

[System.Serializable]
public class BoolEvent : UnityEvent { }

public BoolEvent OnCrouchEvent;
private bool m_wasCrouching = false;

private void Awake()
{
m_Rigidbody2D = GetComponent();

if (OnLandEvent == null)
OnLandEvent = new UnityEvent();

if (OnCrouchEvent == null)
OnCrouchEvent = new BoolEvent();
}

private void FixedUpdate()
{
bool wasGrounded = m_Grounded;
m_Grounded = false;

// The player is grounded if a circlecast to the groundcheck position hits anything designated as ground
// This can be done using layers instead but Sample Assets will not overwrite your project settings.
Collider2D[ ] colliders = Physics2D.OverlapCircleAll(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
for (int i = 0; i < colliders.Length; i++)
{
if (colliders*.gameObject != gameObject)*
{
m_Grounded = true;
if (!wasGrounded)
OnLandEvent.Invoke();
}
}
}
public void Move(float move, bool crouch, bool jump)
{
// If crouching, check to see if the character can stand up
if (!crouch)
{
// If the character has a ceiling preventing them from standing up, keep them crouching
if (Physics2D.OverlapCircle(m_CeilingCheck.position, k_CeilingRadius, m_WhatIsGround))
{
crouch = true;
}
}
//only control the player if grounded or airControl is turned on
if (m_Grounded || m_AirControl)
{
// If crouching
if (crouch)
{
if (!m_wasCrouching)
{
m_wasCrouching = true;
OnCrouchEvent.Invoke(true);
}
// Reduce the speed by the crouchSpeed multiplier
move *= m_CrouchSpeed;
// Disable one of the colliders when crouching
if (m_CrouchDisableCollider != null)
m_CrouchDisableCollider.enabled = false;
} else
{
// Enable the collider when not crouching
if (m_CrouchDisableCollider != null)
m_CrouchDisableCollider.enabled = true;
if (m_wasCrouching)
{
m_wasCrouching = false;
OnCrouchEvent.Invoke(false);
}
}
// Move the character by finding the target velocity
Vector3 targetVelocity = new Vector2(move * 10f, m_Rigidbody2D.velocity.y);
// And then smoothing it out and applying it to the character
m_Rigidbody2D.velocity = Vector3.SmoothDamp(m_Rigidbody2D.velocity, targetVelocity, ref m_Velocity, m_MovementSmoothing);
// If the input is moving the player right and the player is facing left…
if (move > 0 && !m_FacingRight)
{
// … flip the player.
Flip();
}
// Otherwise if the input is moving the player left and the player is facing right…
else if (move < 0 && m_FacingRight)
{
// … flip the player.
Flip();
}
}
// If the player should jump…
if (m_Grounded && jump)
{
// Add a vertical force to the player.
m_Grounded = false;
m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));
}
}
private void Flip()
{
// Switch the way the player is labelled as facing.
m_FacingRight = !m_FacingRight;
// Multiply the player’s x local scale by -1.
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}

Please post your code using CODE tags, or it isn’t really readable. Since the error says basically you can’t do what you are trying to do because you have compile errors, there will be 1 or more compile errors in the Console window. Those are what must be fixed. A compile error will be in red, and it will have the name of the script, some type of descriptive error message, and the line number the error occurs. There’s other information, but what I listed is the most important.

For anyone here to help with your compile errors, you’d need to post the error details I mentioned, make sure you post the correct script, and use CODE tags. See the sticky at the top of the scripting forum if you need help with posting code further. But since it sounds like you weren’t aware of this, I’d encourage you to use the error information to try to first fix the errors yourself.

Note, once you fix 1 compile error you may encounter another. This isn’t because fixing it is causing another, it is because once hitting a compile error the compile process stops, so hasn’t yet encountered the other errors. Work though every compile error until there are 0, then try what you were doing again.

You literally have to make a whole scene and everything and copy and paste the same code, i had this area trying to make steering on a sprite. And it fixed it, I think it is just a bug. You literally just broke unity like me.

I think what might be getting confused here in this thread is that Unity sometimes just throws a compile Error while VisualStudio says the code is fine. In those weired cases (as it happens to me just now) Unity will NOT give a line number or Stacktrace. The only thing it says is which script it thinks has compile errors. Leaves us coders pretty clueless. This might be what the other posters might have encountered…

7720003--968716--20211208_pip_108.png

edit: the script is super NOT complicated and me and VisualStudio agree on it being totally OK. FileName and ClassName match, of course!

I was having this problem with Unity’s new STARTER ASSETS - FPS Controller. The only way to solve was right-clicking → Reimport on top of each script from the package (just 3).

8667078--1167264--upload_2022-12-16_14-2-17.png

1 Like