【问题标题】:give a prefab clones a script from another prefab in Unity3d给一个预制件从 Unity3d 中的另一个预制件中克隆一个脚本
【发布时间】:2015-01-04 16:10:41
【问题描述】:

我有以下:

炮塔球经理:

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

  }
}

球类:

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 秒发射一个球,这个球是一个预制的希望有 Ball 类(当球出界或被触摸时,它应该被销毁)我想做的是创造另一个炮塔每35 sec 希望也应该每 1 秒发射一个球.. 我面临的是休闲问题:

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

  • 图 1:游戏开始时
  • 图 2:35 秒结束时
  • 图 3:您可以看到,在 35 秒结束后,同时创建了数百万个炮塔

【问题讨论】:

    标签: c# unity3d unityscript


    【解决方案1】:

    试试这些脚本。

    TurretExampleManager 脚本附加到游戏对象(例如主摄像机)并分配“turretPrefabballPrefab 游戏对象,然后点击播放


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

    【讨论】:

    • 我在所有脚本中添加了内联 cmets。如果您需要帮助理解,请告诉我
    • 我找出了我的代码中的错误,我非常感谢你的代码我发现了很多东西要添加到我的脚本中,问题出在这一行之后:if (Time.time &gt; LastTurretTime + turretSpawnTime) {我没有更新@987654325 @我忘了添加这一行LastTurretTime =Time.time;,现在一切正常。非常感谢
    猜你喜欢
    • 1970-01-01
    • 2020-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-13
    • 1970-01-01
    • 2019-03-09
    • 2011-03-25
    相关资源
    最近更新 更多