- Home /
Instantiate cloning TOO MANY!
my rocket is shooting one at first key press
then 4 then 10 then like triple then huge amount more?? and after about 5 seconds I am not even able to shoot more nothing comes out..
These scripts are java and c+ 2D game Again this is all 2D !
why is this happening I have 3 scripts on the rocket and prefab rocket
1 rocket
2 forces
3 destrooyrocket
please help and if possible post corrected script and point out what im doing wrong, its clearly something in scripting I think something with addforce and insatiate. I have checked related questions and tutorials.. and for 2d addforce is tricky and different but I cant find what to put ..most info in tutorials is for 3d on addforce not 2d ?
thanks
script 1 rocket--
#pragma strict
public var rocketPrefab : Rigidbody2D;
public var barrelEnd : Transform;
function Update ()
{
if(Input.GetButtonDown("Fire1"))
{
var rocketInstance : Rigidbody2D;
rocketInstance = Instantiate(rocketPrefab, barrelEnd.position, barrelEnd.rotation);
rocketInstance.AddForce(barrelEnd.forward * 3000);
//rigidbody2D.AddForce(Vector3.up.down * Time.deltaTime);
}
}
script 2 forces--
using UnityEngine;
using System.Collections;
public class forces : MonoBehaviour
{
public float moveSpeed = 22f;
void Update()
{
//Jump Script
rigidbody2D.AddForce(Vector2.up * Time.deltaTime);
}
}
script 3 destrooyrocket--
#pragma strict
function Start ()
{
Destroy (gameObject, 1.5f);
}
tanoshimi I think your right so where do I put the instantiate script to? the barrel End transform?
Answer by jeffreymarkbaldridge · Dec 23, 2014 at 04:05 PM
When I run into problems like this in my own code, I like to break it down into the simplest possible solutions. So with that in mind I suggest just focusing on the Instancing issue first, then worry about adding forces. Play around with just creating a single empty object with the correct input. Then when you have that working, start playing with the rigidbodies to get things moving. Then when that is working, add in other behaviours. Programming can become very messy very quickly, and fighting against complexity should be your main objective. It's really difficult to limit one's self to doing one thing at a time, but the result is much cleaner code.
Answer by billyuji · Oct 13, 2016 at 10:55 AM
make sure your clone object itself didn't have a clone object attached.
Dude... This topic is over two years old and the solution has kinda been psoted in the comments ;)
Your answer
Follow this Question
Related Questions
Struggling with Instatiating a prefab 2 Answers
Object name not found? 1 Answer
Instatiate does not work on the client 1 Answer
I'm frustrated, please help. (For cycle + instatiate) 0 Answers