2015-01-04 123 views
0

我有以下幾點:給一個預製克隆一個腳本從另一個預製在Unity3d

TurretBallmanager:

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; 
    } 
    } 
} 

炮塔類:

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>(); 
    } 

    } 
} 

Ball類:

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); 
} 
} 

我有一個炮塔希望f每1秒一個球,這個球是一個預製的願望有球類(當球出界或碰到它應該被銷燬時)我想要做的是創造另一個炮塔每個35 sec希望也應該火了球每1秒。 我所面對的休耕問題:

  1. 炮塔被35 sec後創建的,但它的球都沒有實現ball script和他們沒有被破壞
  2. 溢出由於產生的球的數量而發生並且整個項目處於凍結狀態

As soon as the game begin

When 35 seconds ends

you can see that millions of turrets are created at the same time as soon as 35 sec ends

  • 圖像1:一旦遊戲開始
  • 圖片2:當35秒結束
  • 圖3:可以看到數百萬炮塔在35秒結束時同時產生

回答

0

試試這些腳本。

附上TurretExampleManager腳本到遊戲物體(比如主相機),並指定「turretPrefabballPrefab GameObjects然後點擊播放


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); 
    } 
} 
+0

可以請你解釋你的代碼 – Sora

+0

我加入在線評論中的所有腳本。如果你需要幫助理解,請告訴我 –

+0

我弄清楚了我的代碼中的錯誤,我真的很感激你的代碼,我發現很多東西要添加到腳本中,問題出現在這行之後:'if(Time.time> LastTurretTime + turretSpawnTime){'我沒有更新'LastTurretTime',我忘了添加這行'LastTurretTime = Time.time;',現在一切正常。非常感謝 – Sora

相關問題