Can yall help me with my leaderboard script i cant add an score for the damage dealt

37 Views Asked by At

I have an weapon Script and an Leaderboard script and i wanted to add points whenever i did damage... but in Unity it gives me and Error

Leaderboard Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using Photon.Pun;
using Photon.Pun.UtilityScripts;
using TMPro;

public class Leaderboard : MonoBehaviour
{
   public GameObject playersHolder;

   public float refreshRate = 1f;

   public GameObject[] slots;

   public TextMeshProUGUI[] scoreText;

   public TextMeshProUGUI[] nameTexts;

   private void Start()
   {
     InvokeRepeating(nameof(Refresh), 1f, refreshRate);
   }

   public void Refresh()
   {
    foreach (var slot in slots)
    {
        slot.SetActive(false);
    }

      var sortedPlayerList =
       (from player in PhotonNetwork.PlayerList orderby player.GetScore() descending select player).ToList();

      int i = 0;
      foreach (var player in sortedPlayerList)
      {
        slots[i].SetActive(true);

        

        nameTexts[i].text = player.NickName;
        scoreText[i].text = player.GetScore().ToString();

        i++;
      }

   }

   private void Update()
   {
      playersHolder.SetActive(Input.GetKey(KeyCode.Tab));
   }

}

Weapon Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using TMPro;
using System.Linq;
using UnityEngine.UI;


public class Weapon : MonoBehaviour
{
   
   public int damage;

   public Camera camera;

   public float fireRate;

   public GameObject hitVFX;

   private float nextFire;
   
   public int mag = 3;

   public int ammo = 30;
   public int magAmmo = 30;

   public TextMeshProUGUI magText;
   public TextMeshProUGUI ammoText;

   public Animation animation;
   public AnimationClip reload;

  // [Range(0, 1)]
  // public float recoilPercent = 0.3f;
   [Range(0, 2)]
   public float recoverPercent = 0.7f;

   public float recoilUp = 1f;
   public float recoilBack = 0f;

   private Vector3 originalPosition;

   private Vector3 recoilVelocity = Vector3.zero;

   private bool recoiling;
   public bool recovering;

   private float recoilLength;

   private float recoverLength;

void Start()
{
     magText.text = mag.ToString();
     ammoText.text = ammo + "/" + magAmmo;

     originalPosition = transform.localPosition;

     recoilLength = 0;
     recoverLength = 1 / fireRate * recoverPercent;
}
    // Update is called once per frame
    void Update()
    {
        if (nextFire > 0)
        nextFire -= Time.deltaTime;

        if (Input.GetButton("Fire1") && nextFire <= 0 && ammo > 0 && animation.isPlaying == false)
        {
            nextFire = 1 / fireRate;

            ammo--;

             magText.text = mag.ToString();
        ammoText.text = ammo + "/" + magAmmo;

            Fire();
        }

        if (Input.GetKeyDown(KeyCode.R))
        {
            Reload();
        }

        if (recoiling)
        {
            Recoil();
        }

        if (recovering)
        {
           Recovering();
        }
    }
    void Reload()
    {
        if (mag > 0)
        {
            animation.Play(reload.name);
        }
        
        
        if (mag > 0)
        {
            mag--;

            ammo = magAmmo;
        }

        magText.text = mag.ToString();
        ammoText.text = ammo + "/" + magAmmo;
    }
    
    void Fire()
    {

        recoiling = true;
        recovering = false;

        Ray ray = new Ray(camera.transform.position, camera.transform.forward);
        
        RaycastHit hit;

        if (Physics.Raycast(ray.origin, ray.direction, out hit, 200f))
        {
            PhotonNetwork.Instantiate(hitVFX.name, hit.point, Quaternion.identity);

            if (hit.transform.gameObject.GetComponent<Health>())
            {
                PhotonNetwork.LocalPlayer.AddScore(damage);

                hit.transform.gameObject.GetComponent<PhotonView>().RPC("TakeDamage", RpcTarget.All, damage);
            }
        }
    }

    void Recoil()
    {
        Vector3 finalPosition = new Vector3(originalPosition.x, originalPosition.y + recoilUp, originalPosition.z - recoilBack);

        transform.localPosition = Vector3.SmoothDamp(transform.localPosition, finalPosition, ref recoilVelocity, recoilLength);

        if (transform.localPosition == finalPosition)
        {
            recoiling = false;
            recovering = true;
        }
    }

     void Recovering()
    {
        Vector3 finalPosition = originalPosition;

        transform.localPosition = Vector3.SmoothDamp(transform.localPosition, finalPosition, ref recoilVelocity, recoverLength);

        if (transform.localPosition == finalPosition)
        {
            recoiling = false;
            recovering = false;
        }
    }
    
}

Error: Assets\Weapon.cs(131,43): error CS1061: 'Player' does not contain a definition for 'AddScore' and no accessible extension method 'AddScore' accepting a first argument of type 'Player' could be found (are you missing a using directive or an assembly reference?)

Well im pretty new to Scripting and made these with help from youtube problems... i didnt try much on changed the Using at the beginning

0

There are 0 best solutions below