如何在特定时间点隐藏实例化的游戏对象?

时间:2016-03-17 14:46:33

标签: c# instantiation mesh unity5 gameobject

我在游戏中实例化了四边形(带代码);总体20在y = -2和20在y = 2。现在我想在某个时间点隐藏其中一个并持续一段时间。 我怎样才能做到这一点?我的相机以0.04f的速度移动。 我已经尝试了

plotBars1.GetComponent<MeshRenderer>().enabled=false; 

然而,这隐藏了所有这些。也是我的第二个实例化对象数组(plotBars2)。

这是我脚本的代码:

private GameObject bar1; 
private GameObject bar2; 
private GameObject[] plotBars1;
private GameObject[] plotBars2; 
private Vector3 posBar1; 
private Vector3 posBar2; 




// Use this for initialization
void Start () {


    bar1 = GameObject.CreatePrimitive (PrimitiveType.Quad); //create a Quad
    bar1.transform.position = new Vector3 (-5, -2, 0); 

    bar2 = GameObject.CreatePrimitive (PrimitiveType.Quad); //create a Quad
    bar2.transform.position= new Vector3 (5,2,0);

    //change material to green 
    Material Material= Resources.Load ("Material/green", typeof(Material)) as Material;
    bar1.GetComponent<MeshRenderer>().material = Material; 

    //change material to green 
    bar2.GetComponent<MeshRenderer> ().material = Material;


    //change scale of quad 
    bar1.transform.localScale += new Vector3(5f,-0.5f,0);
    bar2.transform.localScale += new Vector3 (5f, -0.5f, 0); 

    float sizebar1=bar1.GetComponent<MeshRenderer> ().bounds.size.x;
    float sizebar2 = bar2.GetComponent<MeshRenderer> ().bounds.size.x; 


    plotBars1 = new GameObject[20]; //creat an array of  quads.
    plotBars2= new GameObject[20]; //create an array of quads. 


    for (int m = 1; m < 20; m += 2) {

        plotBars1 [m].GetComponent<MeshRenderer> ().enabled = false;

    }




    for (int i = 0; i < 20; i++) {

        int initalposx1 = -5;


        posBar1 = new Vector3 (initalposx1 + (i*20), -2, 0);

        plotBars1 [i] = (GameObject)GameObject.Instantiate (bar1, posBar1, Quaternion.identity);

        plotBars1 [i].active = false; 

        }




    for (int j = 0; j < 20; j++) {

        int initialposx2 = 5; 

        posBar2 = new Vector3 ( initialposx2+ (j*20), 2, 0);

        plotBars2 [j] = (GameObject)GameObject.Instantiate (bar2, posBar2, Quaternion.identity);



    }

}

// Update is called once per frame
void Update () {

}

}

感谢您的帮助。

0 个答案:

没有答案