Clone of clones
When I instantiate my missile prefab, the game creates a copy from the clones that already exist, so when I fire once I have 1 clone, I fire again and I have 3 because the one of the clones if from a clone. The more I fire the more clones of clones are made making the game crash. How would I set it so the missiles only spawn at the original location and not from clones.
using UnityEngine;
using System.Collections;
public class rightSmallMissile : MonoBehaviour {
GameObject prefab1;
public AudioSource bigMisileExp;
public AudioClip impact;
public ParticleSystem exp;
public ParticleSystem aloha;
public float speed = 200.0f;
// Use this for initialization
void Start () {
prefab1 = Resources.Load("rightSmallMissile") as GameObject;
exp = GetComponent<ParticleSystem>();
}
// Update is called once per frame
void Update ()
{
if (Input.GetMouseButtonDown(1))
{
GameObject rightSmallMissile = Instantiate(prefab1, transform.position, transform.rotation) as GameObject;
rightSmallMissile.transform.position = transform.position + Camera.main.transform.forward * 2;
Rigidbody rb = rightSmallMissile.GetComponent<Rigidbody>();
rb.AddForce(transform.forward * 260, ForceMode.Impulse);
Destroy(rightSmallMissile, 4);
}
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name != "rightSmallMissile")
{
speed = 0.0f;
if (speed >= 0.0f)
{
speed = 0.0f;
}
aloha.Play();
transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z);
if (aloha.isPlaying == true)
{
if (!bigMisileExp.isPlaying)
{
bigMisileExp.PlayOneShot(impact, 1);
}
Destroy(gameObject, 1.6f);
}
Destroy(gameObject, 0.6f);
}
}
}
Answer by dank_memes420 · Jun 02, 2016 at 02:32 AM
Never mind I figured out what the problem was. i was instantiating it from the prefab, i created a public GameObject and used that instead. it fixed it up, thanks anyways guys.
Your answer
Follow this Question
Related Questions
How can I have spawned gameObjects be treated as individuals by other scripts? 0 Answers
Changing material dose not change the appearance of the game object 0 Answers
Highlighting all clones instead of the one the mouse is over. 0 Answers
Exponential Instantiation 0 Answers
Only allowing one instance of prefab created by button press & destroying prefab on collision? 0 Answers