对象不断产生预制件

时间:2016-03-16 18:05:23

标签: c# unity3d

我正在使用Unity引擎制作2D游戏......让您了解它的外观,有一个通用的空间背景,4个岩石在屏幕周围弹跳,还有一个领带战斗机。我的目标是让领带战斗机爆炸(我成功地做了),摧毁自己,并使用预制件代替它。我是c#的新手,所以我对API知之甚少。我尝试使用第二个脚本来破坏领带战斗机,然后实例化预制...现在,每当我运行游戏时,它会产生足够的克隆到Unity崩溃的地步,我不知道如何解决它。我尝试使用谷歌搜索和手动修复(因此bools),但似乎没有任何工作。任何帮助将不胜感激。另外,请不要只评论;把它写成答案,这样我就可以正确标记它是否有效。这是脚本(我假设错误在第二个,但我包括两个供参考):

第一个脚本:

using UnityEngine;
using System.Collections;

public class tfScript : MonoBehaviour 
{

Vector3 tfPos;
Vector3 worldPos;
float mousePosInBlocksx;
float mousePosInBlocksy;
int lives;
public Sprite tieFight; // Drag your first sprite here
public Sprite kaboom; // Drag your second sprite here
private SpriteRenderer spriteRenderer;
float makeThingsGoBoom;

// Use this for initialization
public void Start () 
{
    tfPos = new Vector3 (3f, 3f, -4f);
    lives = 20;
    spriteRenderer = GetComponent<SpriteRenderer>(); // we are accessing the SpriteRenderer that is attached to the Gameobject

    if (spriteRenderer.sprite == null) // if the sprite on spriteRenderer is null then
    {
        spriteRenderer.sprite = tieFight; // set the sprite to sprite1
    }
}

// Update is called once per frame
public void Update () 
{
    GameObject controller = GameObject.Find ("controller");
    gameController gameCon = controller.GetComponent<gameController> ();

    mousePosInBlocksx = ((Input.mousePosition.x / Screen.width) * 16);
    mousePosInBlocksy = ((Input.mousePosition.y / Screen.width) * 12)+2;

    tfPos.x = Mathf.Clamp (mousePosInBlocksx, .5f, 15.5f);
    tfPos.y = Mathf.Clamp (mousePosInBlocksy, .5f, 11.5f);

    this.transform.position = tfPos;
    if (makeThingsGoBoom == 0) 
    {   
        gameCon.Update();
    }

}

public void ChangeTheDarnSprite ()
{

    if (spriteRenderer.sprite == tieFight) { // if the spriteRenderer sprite = sprite1 then change to sprite2
        spriteRenderer.sprite = kaboom;
    } 
    else 
    {
        spriteRenderer.sprite = tieFight;
    }
}

public void OnCollisionEnter2D (Collision2D collider)
{
    if (collider.gameObject.name.Contains("spacerock")  ) 
    {
        lives--;
        print (getLives ());

    }

    if (collider.gameObject.name.Contains("spacerock")) // If the space bar is pushed down
    {

        spriteRenderer.sprite = kaboom;
        makeThingsGoBoom = 0;
    }
}

public void increaseLives()
{
    lives++;
}

public double getLives()
{
    return lives;
}


}

第二个脚本:

using UnityEngine;
using System.Collections;

public class gameController : MonoBehaviour 
{
public GameObject tf;
public GameObject tfpf;
public bool iBlowedUp = false;

public void Start()
{

}

public void Update () 
{
    boom ();
}

public void boom()
{
        iBlowedUp = true;

        if (iBlowedUp = true) 
        {
            StartCoroutine (waitForIt ());
            Destroy (tf);
            tfpf = Instantiate (Resources.Load ("Prefabs/tfpf")) as GameObject;
            iBlowedUp = false;
        }
}

public IEnumerator waitForIt()
{
    print ("Bob lives #2!");
    yield return new WaitForSeconds (1); 
    print ("John is a turtle #2!");
}
}

2 个答案:

答案 0 :(得分:1)

您正在Update函数中调用以下方法,该函数会不断执行。

public void boom()
{
        iBlowedUp = true;

        if (iBlowedUp = true) 
        {
            StartCoroutine (waitForIt ());
            Destroy (tf);
            tfpf = Instantiate (Resources.Load ("Prefabs/tfpf")) as GameObject;
            iBlowedUp = false;
        }
}

如果iBlowedUp = true; if (iBlowedUp = true){没有意义,因为该陈述始终为真。

它应该类似于:

public void boom()
{
        if (iBlowedUp == true) 
        {
            iBlowedUp = false;
            StartCoroutine (waitForIt ());
            Destroy (tf);
            tfpf = Instantiate (Resources.Load ("Prefabs/tfpf")) as GameObject;
        }
}

您可能希望在其他地方将iBlowedUp设置为true。正如我在tfScript.Update()方法中考虑的那样,而不是调用Update方法。

if (makeThingsGoBoom == 0) 
{   
    gameCon.iBlowedUp = true;
    //gameCon.Update();
}

答案 1 :(得分:0)

double

在实例化make isBlowedUp false之前,我不能说ı很明白这个“Unity崩溃”。你的代码有些复杂。我希望你也能修好它们