How can i fix this error code?

he's giving me an error code for private?
No description
15 Replies
SinFluxx
SinFluxx2y ago
What are the lines the errors are referencing? (the error from the sounds of it is pretty clear though)
Maximilian Damm
Maximilian DammOP2y ago
No description
Maximilian Damm
Maximilian DammOP2y ago
im new so i dont really know how tofix this do you need anything else?
SinFluxx
SinFluxx2y ago
Can you send the whole class? Also paste is using $code these instructions:
MODiX
MODiX2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
Maximilian Damm
Maximilian DammOP2y ago
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

private float horizontal;
private float speed = 8f;
private float jumpingPower = 16f;
private bool isFacingRight = true;

[SerializeField] private Rigidbody2D rb;
[SerializeField] private Transform groundCheck;
[SerializeField] private LayerMask groundLayer;


public class player : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
horizontal = Input.GetAxisRaw("Horizontal");

if (Input.GetButtonDown("Jump") && IsGroundedGrounded())
{
rb.velocity = new Vector2(rb.velocity.x, jumpingPower);
}

if (Input.GetButtonUp("Jump") && rb.velocity.y > 0f)
{
rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
}

Flip();
}

private void FixedUpdate()
{
rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
}

private bool IsGrounded()
{
return Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer);
}

private void Flip()
{
if (isFacingRight && horizontal < 0f || !isFacingRight && horizontal > 0f)
{
isFacingRight = isFacingRight;
Vector3 localScale = transform.localScale;
localScale.x *= -1f;
trasform.localScale = localScale;
}
}


}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

private float horizontal;
private float speed = 8f;
private float jumpingPower = 16f;
private bool isFacingRight = true;

[SerializeField] private Rigidbody2D rb;
[SerializeField] private Transform groundCheck;
[SerializeField] private LayerMask groundLayer;


public class player : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
horizontal = Input.GetAxisRaw("Horizontal");

if (Input.GetButtonDown("Jump") && IsGroundedGrounded())
{
rb.velocity = new Vector2(rb.velocity.x, jumpingPower);
}

if (Input.GetButtonUp("Jump") && rb.velocity.y > 0f)
{
rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
}

Flip();
}

private void FixedUpdate()
{
rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
}

private bool IsGrounded()
{
return Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer);
}

private void Flip()
{
if (isFacingRight && horizontal < 0f || !isFacingRight && horizontal > 0f)
{
isFacingRight = isFacingRight;
Vector3 localScale = transform.localScale;
localScale.x *= -1f;
trasform.localScale = localScale;
}
}


}
just a simple script for a 2d character
cap5lut
cap5lut2y ago
your fields are outside of the player class, thats why u get that error
Maximilian Damm
Maximilian DammOP2y ago
now this dosnt work
No description
SinFluxx
SinFluxx2y ago
You haven't moved them inside your class?
Maximilian Damm
Maximilian DammOP2y ago
in the player class?
SinFluxx
SinFluxx2y ago
yes, like cap5lut said you can't have fields outside of a class
Maximilian Damm
Maximilian DammOP2y ago
ok every thing works now, thank you
LordoToasty
LordoToasty2y ago
hi, I have a .NET proj wich needs help beeing fixed, am I right here?
SinFluxx
SinFluxx2y ago
@LordoToasty you should create your own thread or ask in #help-0
LordoToasty
LordoToasty2y ago
I have, thanks.

Did you find this page helpful?