如何在unity3d中将硬币分数添加到当前分数

如何在unity3d中将硬币分数添加到当前分数,unity3d,Unity3d,如何通过主分数增加硬币价值?这意味着,如果玩家收集硬币,他将得到25分,每一个硬币附加到当前的分数 using UnityEngine; using System.Collections; public class PlayerScript : MonoBehaviour { public AudioSource audio1; public AudioSource audio2; public float offsetY = 40f; public flo

如何通过主分数增加硬币价值?这意味着,如果玩家收集硬币,他将得到25分,每一个硬币附加到当前的分数

using UnityEngine; 
using System.Collections;

public class PlayerScript : MonoBehaviour {

    public AudioSource audio1;
    public AudioSource audio2;
    public float offsetY = 40f;
    public float sizeX = 100;
    public float sizeY = 40;
    public Vector3 speed = new Vector3 (5f, 0f, 0f);
    public Vector3 jumpForce = new Vector3 (0f, 5f, 0f);
    private bool jump = true;
    private bool grounded = false;
    private bool doubleJump = false;

    void Start(){
        AudioSource[] audios = GetComponents<AudioSource>();
        audio1 = audios[0];
        audio2 = audios[1];
    }

    void FixedUpdate(){
        if (grounded)
            doubleJump = false;
    }

    void Update(){
        transform.Rotate (-5,15,10*Time.deltaTime); //rotates 50 degrees per second around z axis
        if ((grounded || !doubleJump) && jump) {
            rigidbody.AddForce (speed, ForceMode.Acceleration);

            // if(Input.GetMouseButtonDown(0))
            if (Input.GetButtonDown ("Jump"))
            {
                //Debug.Log("Hit da floor");
                rigidbody.AddForce (jumpForce, ForceMode.VelocityChange);

                if(!grounded)
                    doubleJump = true;
            }
        }
        //grounded = false;  
    }

    void OnCollisionEnter(Collision obj) {
        grounded = true;
        audio1.Play();
    }

    void OnCollisionExit(Collision obj) {
        grounded = false;
    }

    void OnTriggerEnter(Collider Coin){
        audio2.Play ();
        Destroy (Coin.gameObject);
    }

    void OnGUI() {
        GUI.color = Color.black;

        int currentScore = (int)transform.localPosition.x;
        int highScore = PlayerPrefs.GetInt ("highS", 0);

        if (currentScore > highScore) {
            PlayerPrefs.SetInt ("highS", currentScore);
        }
        GUI.Label (new Rect (Screen.width/4, offsetY, sizeX, sizeY ), "<color=red> Score: </color>" +"<color=white>" + currentScore + "</color>");
        GUI.Label (new Rect (Screen.width/21, offsetY, sizeX, sizeY ), "<color=red>Highscore:</color> " +"<color=white>" + highScore + "</color>");
        //GUI.Label (new Rect (Screen.width/11, offsetY, sizeX, sizeY ), "Coins: " + coins);
    }

假设根据您的评论,您希望每枚硬币都有一个价值:

public class Coin:MonoBehaviour {
  public int coinVal=25; // default value, you can change it per each coin in the inspector
}
在每枚硬币上添加一个单一行为:

public class Coin:MonoBehaviour {
  public int coinVal=25; // default value, you can change it per each coin in the inspector
}
并在PlayerScript中更改OnTiggerCenter:

在这里,我在您的课堂上引入了一个分数字段,因为您在OnGUI中使用的这个字段看起来像一个临时补丁

您必须将硬币游戏对象设置为触发器,查看文档:

另外,请注意,你不能在触发时销毁它所触及的一切,目前它确实如此