块变得不可见(unity2d,c#) [英] Blocks going invisible (unity2d, c#)

查看:99
本文介绍了块变得不可见(unity2d,c#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个脚本,允许我随机生成我角色前面的平台,代码是:



I'm attempting to create a script which allows me to randomly generate platforms infront of my character and the code is:

using UnityEngine;
using System.Collections;

public class SpawnScript : MonoBehaviour
{

    public GameObject[] obj;
    public float spawnMin = 1f;
    public float spawnMax = 2f;

    // Use this for initialization
    void Start()
    {
        Spawn();
    }

    void Spawn()
    {
        Instantiate(obj[Random.Range(0, obj.GetLength(0))], transform.position, Quaternion.identity);
        Invoke("Spawn", Random.Range(spawnMin, spawnMax));
    }

}







这实际上有效。但由于某种原因,它只能使用一段时间,然后我使用的块变得不可见,尽管它们仍然在那里,我仍然可以在它们上运行。问题可能是我将它作为孩子分配到相机,但奇怪的是它工作了几秒钟,然后它继续创建相同的块但不可见。



可能是另一个负责这个的脚本?也许它以某种方式摧毁了块渲染器?如果是这样的话,我只能想到我的脚本会破坏块,但是它只能破坏播放器后面的块,因为运行脚本的对象被附加到我的相机并破坏了它接触到的所有内容但它被放置了在我的球员后面的固定位置。所以它不应该删除它前面的对象上的渲染器。也许我的代码有问题?






This acctually works. But for some reason it only works for a while, then the blocks I'm using turns invisible altough they are still there and I can still run on them. The issue might be that I have it assigned as a child to the camera, but the wierd thing is that it's working for a few seconds and then it proceeds to creating the same blocks but invisible.

It might be another script which is responsible for this? Maybe it's somehow destroying the blocks renderer? If so I can only think of my script which destroys blocks, but it's only supposed to destroy blocks that are behind the Player, since the object that is running the script is attached to my camera and destroys everything it comes in contact with but it's placed in a fixed position behind my player. so it shouldn't remove the renderer on objects infront of it. Maybe it's something wrong in my code?

using UnityEngine;
using System.Collections;

public class DestroyerScript : MonoBehaviour
{

    void OnTriggerEnter2D(Collider2D other)
    {

        if (other.tag == ("Player"))
        {
            Debug.Break();
            return;
        }

        if (other.gameObject.transform.parent)
        {
            Destroy(other.gameObject.transform.parent.gameObject);
        }
        else
        {
            Destroy(other.gameObject);
        }
    }
}





我尝试了什么:



好​​吧我不知道出了什么问题但是我已经尝试过使用我的设置但无济于事。



What I have tried:

Well I don't know what is wrong But i have tried finnecking around with my settings but to no avail.

推荐答案

你在spawn的调用中调用了spawn。



你的代码完全可重入...每隔一到两秒就无限制地创建对象彼此......闪闪发光



看看你的层次结构选项卡,你得到越来越多的产卵



这里是你一步一步的代码



1.)程序启动调用spawn

2.)spawn实例化一个随机的副本来自obj列表的对象

3.)Spawn然后说1 - 2秒后调用自己(产卵)

4.)1-2秒传递spawn被调用( AKA转到步骤2)



因此,每隔1-2秒重复创建随机对象,所有对象都在实例化同一点是transform.position AKA持有这个脚本的对象的位置。



脚本说要做什么,而且很多对象都在彼此的ontop上使屏幕闪烁。我怀疑你不希望他们在彼此的顶部,或者至少不想要它们的无限量,这就是目前发生的事情。



至少让我们通过添加一个可以设置的计数来停止无穷大

You have invoked spawn within the call of spawn.

Your code goes fully reentrant .. creating objects every one to two seconds infinitely on top of each other ... HENCE THE FLASHING

Look at your hierarchy tab you get ever increasing numbers of spawn

Here is you code step by step

1.) Procedure start calls spawn
2.) spawn Instantiates a copy of a random object from the obj list
3.) Spawn then says to invoke itself (spawn) 1 - 2 seconds later
4.) 1-2 seconds passes spawn is called (AKA goto step 2)

So random objects are being created repeatedly every 1-2 seconds all instantiated on the same point being transform.position AKA the position of the object holding this script.

That what the script says to do and that many objects all ontop of each other will make the screen flash. I suspect you don't want them ontop of each other or at least don't want infinite amount of them which is what happens at the moment.

Lets at least stop the infinity by adding a count which you can set
using UnityEngine;
using System.Collections;
 
public class SpawnScript : MonoBehaviour
{
 
    public GameObject[] obj;
    public float spawnMin = 1f;
    public float spawnMax = 2f;
    public int objCount = 0;   // Yeah we are going to count the objects you make
 
    // Use this for initialization
    void Start()
    {
        Spawn();
    }
 
    void Spawn()
    {
        Instantiate(obj[Random.Range(0, obj.GetLength(0))], transform.position, Quaternion.identity);
        // Only if objCount less than 10 make a new one (stops infinity loop)
        if (objCount < 10) Invoke("Spawn", Random.Range(spawnMin, spawnMax));
        // Increment objCount
        objCount++;
    }
 
}


这篇关于块变得不可见(unity2d,c#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆