How to fix 'public' is not valid for this item

hi, Im new to coding and i need help with something big.

Im trying to make my character use the animations Idle, Move and attacked when doing the movement, but when i add public to my code it says: ‘public’ is not valid for this item.

public class Player : MonoBehaviour
{

//Variables

// player
public float movementSpeed;
Animation anim;

public float attackTimer;
private float currentAttackTimer;

private bool moving;
private bool attacking;
private bool followingEnemy;

private float damage;
public float minDamage;
public float maxDamage;

private bool attacked;

// pmr
public GameObject playerMovePoint;
private Transform pmr;
private bool triggeringPMR;

//enemy
private bool triggeringEnemy;
private GameObject attackingEnemy;

//Functions

void Start()
{
pmr = Instantiate(playerMovePoint.transform, this.transform.position, Quaternion.identity);
pmr.GetComponent().enabled = false;
anim = GetComponent();
currentAttackTimer = attackTimer;

}
void Update()
{
//Player Movement
Plane playerPlane = new Plane(Vector3.up, transform.position);
Ray ray = UnityEngine.Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
float hitDistance = 0.0f;

if (playerPlane.Raycast(ray, out hitDistance))
{
Vector3 mousePosition = ray.GetPoint(hitDistance);
if (Input.GetMouseButtonDown(0))
{
moving = true;
triggeringPMR = false;
pmr.transform.position = mousePosition;
pmr.GetComponent().enabled = true;

if (Physics.Raycast(ray, out hit))
{
if(hit.collider.tag == “Enemy”)
{
attackingEnemy = hit.collider.gameObject;
followingEnemy = true;
} else {
attackingEnemy = null;
followingEnemy = false;
}
}

}
}

if (moving)
Move();
else
{
if (attacking)
Attack();
else
Idle();

}

if (triggeringPMR)
{
moving = false;
}

if (triggeringEnemy)
Attack();

if (attacked)
{
currentAttackTimer -= 1 * Time.deltaTime;
}

if (currentAttackTimer <= 0)
{
currentAttackTimer = attackTimer;
attacked = false;
}

public void Idle()
{
anim.CrossFade(“idle”);
}
public void Move()
{
if (followingEnemy)
{
transform.position = Vector3.MoveTowards(transform.position, attackingEnemy.transform.position, movementSpeed);
this.transform.LookAt(attackingEnemy.transform);
} else {
transform.position = Vector3.MoveTowards(transform.position, pmr.transform.position, movementSpeed);
this.transform.LookAt(pmr.transform);
}

anim.CrossFade(“walk”);
}

public void Attack()
{
if (!attacked)
{
damage = Random.Range(minDamage, maxDamage);
print(damage);

attacked = true;
}
anim.CrossFade(“attack”);
transform.LookAt(attackingEnemy.transform);

}

void OnTriggerEnter(Collider other)
{
if (other.tag == “PMR”)
{
triggeringPMR = true;
}

if (other.tag == “Enemy”)
{
triggeringEnemy = true;
}
}
void OnTriggerExit(Collider other)
{
if (other.tag == “PMR”)
{
triggeringPMR = false;
}

if (other.tag == “Enemy”)
{
triggeringEnemy = false;
}

}

}
}

Please use code tags and point out the exact line where you are getting the error.

Odds are it’s a mistake with your curly braces {}, but I’m not reading 170 lines of no-indentation code to try to figure out where the mismatch is.

2 Likes

the problem is in
-public void Idle()
-public void Move()
-public void Attack()
it says that the modifier public is not valid for this item.

{
anim.CrossFade("idle");
}
public void Move()
{
if (followingEnemy)
{
transform.position = Vector3.MoveTowards(transform.position, attackingEnemy.transform.position, movementSpeed);
this.transform.LookAt(attackingEnemy.transform);
} else {
transform.position = Vector3.MoveTowards(transform.position, pmr.transform.position, movementSpeed);
this.transform.LookAt(pmr.transform);
}

anim.CrossFade("walk");
}

public void Attack()
{
if (!attacked)
{
damage = Random.Range(minDamage, maxDamage);
print(damage);

attacked = true;
}
anim.CrossFade("attack");
transform.LookAt(attackingEnemy.transform);

}```

Like Antistone said, it’s your curly braces.

See this?

if (currentAttackTimer <= 0)
{
currentAttackTimer = attackTimer;
attacked = false;
}

public void Idle()
{
anim.CrossFade("idle");
}

Your If statement needs to be inside a function.
and your void function needs to be inside your class and not inside another function.
So, those 2 things shouldn’t be inside the same scope.

public class someClass { //Start Of Class Scope
 //...
 //Class Scope
 void Update { //Start Of Function Scope
  //...
  //Function Scope
  if (something) { //Start Of IF Scope
   //...
  } //End Of IF Scope

 } //End Of Function Scope

 //Class Scope
 public void Idle { //Start of Function Scope
  //...
 } //End of Function Scope

} //End Of Class Scope

When your indentation gets all messed up, fix the problem, don’t keep typing.