Hi,
I need help with this script,
at the end of it, it continues to fails to succeed.
Please post code as text and embed it in code tags rather than file attachement.
Similar for error messages, you can copy paste the contents of the selected console logs as text.
You wrote the bottom most methods outside the scope (brackets) of the class. Properly formatting code helps with this, and the IDE can help you autoformat your code. That makes these issues easy to spot.
(console)
Assets\Scripts\PlayerController.cs(187,1): error CS1022: Type or namespace definition, or end-of-file expected
Assets\Scripts\PlayerController.cs(187,3): error CS1022: Type or namespace definition, or end-of-file expected
(code) (playercontroller)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
public class PlayerController : MonoBehaviourPun
{
// each player will have unique ID
public int id;
[HideInInspector]
[Header("info")]
public float moveSpeed;
public int gold;
public int currentHp;
public int maxHp;
public bool dead;
[Header("Attack")]
public int damage;
public float AttackRange;
public float attackDelay;
public float LastAttackTime;
[Header("Components")]
public Rigidbody2D rig;
public Player photonPlayer;
public SpriteRenderer sr;
public Animator weaponAnim;
//local player
public static PlayerController me;
private object headerInfo;
private int maxHP;
[PunRPC]
public void Initialize(Player player)
{
id = player.ActorNumber;
photonPlayer = player;
//add ourselves to the players array in the intialize function.
GameManager.instance.players[id - 1] = this;
// initialize the health bar
HeaderInfo.Initialized(player.NickName, maxHP);
if (player.IsLocal)
me = this;
else
rig.isKinematic = false;
}
private void Update()
{
if (!photonView.IsMine)
return;
Move();
if (Input.GetMouseButtonDown(0) && Time.time - LastAttackTime > attackDelay)
Attack();
float mouseX = (Screen.width / 2) - Input.mousePosition.x;
if (mouseX < 0)
weaponAnim.transform.parent.localScale = new Vector3(1, 1, 1);
else
weaponAnim.transform.parent.localScale = new Vector3(-1, 1, 1);
}
void Move()
{
// get the horizontal and vertical inputs
float x = Input.GetAxis("Horizontal");
float y = Input.GetAxis("Vertical");
// apply the velocity to player
rig.velocity = new Vector2(x, y) * moveSpeed;
}
void Attack()
{
LastAttackTime = Time.time;
Vector3 dir = (Input.mousePosition - Camera.main.ScreenToWorldPoint(transform.position).normalized);
//shoot raycast
RaycastHit2D hit = Physics2D.Raycast(transform.position + dir, dir, AttackRange);
//if we hit enemy
if (hit.collider != null && hit.collider.gameObject.CompareTag("Enemy"))
{
// get the enemy and damage them
Enemy enemy = hit.collider.GetComponent<Enemy>();
enemy.photonView.RPC("TakeDamage", RpcTarget.MasterClient, damage);
}
//Player Attack Animation
weaponAnim.SetTrigger("Attack");
}
[PunRPC]
public void TakeDamage(int damage)
{
currentHp -= damage;
// Update the health bar
if (currentHp <= 0)
Die();
else
{
photonView.RPC("FlashDamage", RpcTarget.All);
}
}
void FlashDamage()
{
StartCoroutine(DamageFlash());
IEnumerator DamageFlash()
{
sr.color = Color.red;
yield return new WaitForSeconds(0.04f);
sr.color = Color.white;
}
}
void Die()
{
dead = true;
rig.isKinematic = true;
//we want player disapear from scene and move them far away from scene
transform.position = new Vector3(0, 99, 0);
//player should spawn in scene after a period of time again
Vector3 spawnPos = GameManager.instance.spawnPoints[Random.Range(0, GameManager.instance.spawnPoints.Length)].position;
_ = StartCoroutine(Spawn(spawnPos, GetRespawnTime()));
dead = false;
transform.position = spawnPos;
currentHp = maxHp;
rig.isKinematic = false;
//update the healthBar
object p = headerInfo.photonView.RPC("UpdateHealthBar", RpcTarget.All, currentHp);
float GetRespawnTime()
{
return GameManager.instance.respawnTime;
}
//update our health bar
}
private static IEnumerator Spawn(Vector3 spawnPos, float timeToSpawn)
{
yield return new WaitForSeconds(timeToSpawn);
}
}
// healing player
[PunRPC]
void Heal(int amountToHeal, int currentHp, int maxHp)
{
currentHp = Mathf.Clamp(currentHp + amountToHeal, 0, maxHp);
//update the Health bar
}
// gold function
[PunRPC]
void GetGold(int goldToGive)
{
int gold = 0;
gold += goldToGive;
// update the UI
}
{ }
"
The error gives you everything you need. Line 187, Column 1. Look there. You’ve added a “{ }” which isn’t valid for any reason in C#. You should be closing the class with “}” only but you already closed the class on Line 166 with “}” which is wrong because then you have methods below it outside the class.
To be honest, doing multiplayer anything without understanding the basic syntax principle is very surprising and is a certainly a case of getting ahead of your current skills. It’s obviously up to you but I would take a little time to familiarise yourself with the basics of C# i.e. how classes are created and methods/properties/fields inside them are added.