给预制件从 Unity3d 中的另一个预制件克隆脚本

give a prefab clones a script from another prefab in Unity3d

我有以下内容:

炮塔球管理员:

using UnityEngine;
using System.Collections;

public class TurretBallManager : MonoBehaviour {

// Use this for initialization
public GameObject BallPrefab;
public GameObject TurretPrefab;
public static TurretBallManager instance;
public int turretSpawnTime=35;
public int LastTurretTime=0;
Vector2 v;
void Start () {
    instance = this;
    v = new Vector2(TurretPrefab.transform.position.x,TurretPrefab.transform.position.y);
}

// Update is called once per frame
void Update () {
    if (Time.time > LastTurretTime + turretSpawnTime) {
        GameObject T = Instantiate(TurretPrefab,v,Quaternion.identity) as GameObject;
        //T.AddComponent<Turret>();
        v.x=T.transform.position.x+2;
    }
  }
}

炮塔class:

using UnityEngine;
using System.Collections;

public class Turret : MonoBehaviour {

// Use this for initialization
double LastBallTime=0.0;
double LastTurretTime=0.0;
public decimal spawnballTime=1.5;
Vector2 v ;
void Start () {

}

// Update is called once per frame
void Update () {
    if (Time.time > LastBallTime + spawnballTime) {
        LastBallTime=Time.time;     
        Debug.Log (transform.position);
        GameObject B = Instantiate(TurretBallManager.instance.BallPrefab, transform.position, transform.rotation) as GameObject;
        //B.AddComponent<Ball>();
    }

  }
}

球class:

using UnityEngine;
using System.Collections;

public class Ball : MonoBehaviour {

// Use this for initialization
void Start () {

}
void OnMouseDown() {
    Object.Destroy (gameObject);
}
// Update is called once per frame
void Update () {

}


void OnBecameInvisible ()
{
    Debug.Log ("destroyed");
    Destroy(gameObject);
}
}

我有一个炮塔希望每 1 秒发射一个球,球是一个预制件希望有球 class(当球出界或被触及时它应该被摧毁)我是什么我想做的是每隔 35 sec 创建一个炮塔,希望每 1 秒发射一个球 .. 我面临的是休闲问题 :

  1. 炮塔是在 35 sec 之后创建的,但它的球没有实现 ball script 并且它们没有被摧毁
  2. 由于创建的球数量过多导致溢出,整个项目冻结

试用这些脚本。

TurretExampleManager 脚本附加到游戏对象(比如主摄像机)并分配“turretPrefabballPrefab GameObjects 然后点击 Play


TurretExampleManager.cs

using UnityEngine;
using System.Collections;

public class TurretExampleManager : MonoBehaviour {

    public static TurretExampleManager instance;

    //Maximum number of turrets that can spawn
    public int maxTurrets = 10;

    //The current number of turrets spawned
    private int currentTurrets = 0;

    //The time between turret spawns
    public float turretSpawnTime = 35;

    //Current spawn timer for turret
    public float thisTurretSpawnTime = 35;

    //Assign this prefab as the turret
    public GameObject turretPrefab;

    //Assign this prefab as the ball
    public GameObject ballPrefab;


    // Use this for initialization
    void Start () {
        //Creating a static instance of the TurretExampleManager class.
        //This is used in the BallScript.cs class to access the Ball Prefab
        instance = this;

        //Assigning the decrementing time counter the same as the time between spawns
        thisTurretSpawnTime = turretSpawnTime;
    }

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

        //Let's first check if we have the maximum number of turrets already (10 in this case)
        if(currentTurrets < maxTurrets) {

             //We have fewer than 10 turrets, so let's reduce the current time counter
             thisTurretSpawnTime -= Time.deltaTime;

            //The current time counter has hit 0, so we need to create a new turret
            if(thisTurretSpawnTime <= 0) {

                SpawnNewTurret();                       //Spawn a new turret
                thisTurretSpawnTime = turretSpawnTime;  //Reset the time
            }
        }
    }

    public void SpawnNewTurret () {
        //Increment the current number of turrets
        currentTurrets++;

        //Create a random position to spawn the new turret at
        Vector3 randomPosition = new Vector3(Random.Range(-10, 10), 0, Random.Range(-10, 10));

        //Instantiate a new turret
        GameObject thisTurret = Instantiate(turretPrefab, randomPosition, Quaternion.identity) as GameObject;

        //Add the Turret Script
        thisTurret.AddComponent<TurretScript>();
    }
}


TurretScript.cs

using UnityEngine;
using System.Collections;

public class TurretScript : MonoBehaviour {

    //This variable controls the rate of ball spawn
    public float ballFireTime = 1;

    //Counter for time.
    public float thisBallFireTime = 1;

    // Use this for initialization
    void Start () {
        thisBallFireTime = ballFireTime;
    }

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

        //Reduce the time 
        thisBallFireTime -= Time.deltaTime;

        //If the time reaches 0, we need to spawn a new ball
        if(thisBallFireTime <= 0) {

            //Reset the ball spawn time
            thisBallFireTime = ballFireTime;

            //Instantiate a new ball
            GameObject thisBall = Instantiate(TurretExampleManager.instance.ballPrefab, transform.position, Quaternion.identity) as GameObject;

            //Add the Ball Script to the newly spawned ball
            thisBall.AddComponent<BallScript>();
        }
    }
}


BallScript.cs

using UnityEngine;
using System.Collections;

public class BallScript : MonoBehaviour {

    private Vector3 randomDirection = Vector3.zero;

    void Start () {

        //Create a random direction for the ball to move in
        randomDirection = new Vector3(Random.Range(-1, 1), 0, Random.Range(-1, 1));
    }

    void OnMouseDown() {
        Debug.Log ("Destroyed because out of click");
        Destroy(gameObject);
    }

    void OnBecameInvisible () {
        Debug.Log ("Destroyed because out of bounds");
        Destroy(gameObject);
    }

    void Update () {
        //Move the ball in the random direction generated
        transform.Translate(randomDirection * Time.deltaTime);
    }
}