繁体   English   中英

我的代码有些问题

[英]I have some issues with my code

这段代码来自我正在制作的游戏。 目的是收集火箭部件。 收集零件时,它们会在之后消失,但是当您尝试收集第二个零件时,它会将其添加到零件变量中。

using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
using UnityEngine;

public class Collection : MonoBehaviour {

private int Parts;
public Text CountPart;
private string Amount;

void Start ()
{
    Parts = 0;
    SetPartText();
}

// Update is called once per frame
void Update () {
    if (Parts == 10)
    {
        Application.LoadLevel("DONE");
    }
}

void OnMouseDown ()
{
    gameObject.SetActive(false);
    Parts = Parts + 1;
    SetPartText();
}

void SetPartText ()
{
    Amount = Parts.ToString () + "/10";
    CountPart.text = "Rocket Parts Collected: " + Amount;
}
}

首先,你需要考虑这里的用例,你想收集火箭部件,然后隐藏/销毁它们,并在你的游戏对象中添加一个部件计数。

但是您当前的代码存在一个问题,即您停用当前收集零件的玩家,因此当您收集第一部分时,您将停用该玩家,这样您就无法收集其他项目。

解决方案是获取您收集的部分的引用,然后使其成为setActive(false)

using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
using UnityEngine;

public class Collection : MonoBehaviour {

private int Parts;
public Text CountPart;
private string Amount;
public GameObject collectedGameObject;
void Start ()
{
    Parts = 0;
    SetPartText();
}

// Update is called once per frame
void Update () {
    if (Parts == 10)
    {
        Application.LoadLevel("DONE");
    }
}

void OnMouseDown ()
{
        //here you need to initialize the collectedGameObject who is collected. you can use Raycasts or Colliders to get the refrences.
    collectedGameObject.SetActive(false);
    Parts = Parts + 1;
    SetPartText();
}

void SetPartText ()
{
    Amount = Parts.ToString () + "/10";
    CountPart.text = "Rocket Parts Collected: " + Amount;

}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM