Sora
Sora

Reputation: 2551

give a prefab clones a script from another prefab in Unity3d

I have the following:

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

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

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

I have a turret that I want to fire a ball every 1 sec. The ball is a prefab with the "Ball" class, and when the ball is out of bounds or is touched it should be destroyed.

What I'm trying to do is create a another turret every 35 sec which also should fire a ball every 1 sec

What I am facing are the following problems:

  1. the turret is being created after 35 sec but it's ball are not implementing the ball script and they are not being destroyed
  2. An overflow is occurring due to the number of balls created and the whole project is freezing

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

Upvotes: 0

Views: 819

Answers (1)

Venkat at Axiom Studios
Venkat at Axiom Studios

Reputation: 2516

Try these scripts out.

Attach the TurretExampleManager script to a GameObject (say Main Camera) and assign the "turretPrefab and the ballPrefab GameObjects then hit 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);
    }
}

Upvotes: 1

Related Questions