clone, instantiates 2 of my prefabs instead of 1
using UnityEngine;
using System.Collections;
public class shooter2 : MonoBehaviour
{
public GameObject[] projectiles;
int projectilesNo;
public float speedFactor;
public float delay;
public float delayTimer = 2f;
float timer;
// Use this for initialization
void Start()
{
timer = delayTimer;
}
// Update is called once per frame
void Update()
{
timer -= Time.deltaTime;
if (Input.GetMouseButtonDown(0) && timer <= 0)
{
GameObject clone = (GameObject)Instantiate(projectiles[projectilesNo], transform.position, Quaternion.identity);
clone.GetComponent<Rigidbody2D>().velocity = -transform.right * speedFactor;
projectilesNo = Random.Range(0, 2);
timer = delayTimer;
}
}
IEnumerator Shoots()
{
while (true)
{
yield return new WaitForSeconds(delay);
}
}
}
heres the script, im trying to instantiate two different projectiles while moving them while spawning them with random. range. it works it just spawning both of the projectiles at once can anyone tell me why its doing this. im trying to get them to instantiate individually thanks
Answer by Pengocat · Jan 11, 2017 at 08:43 PM
Maybe you got more than one instance of the script in the scene? You could add Debug.Log(GetInstanceID());
inside the if statement. There should only be one unique ID printing.
when i add Debug.Log(GetInstanceID()); they shoot individually but one of the prefabs doesnt show up now its says in my 162930 UnityEngine.Debug:Log(Object) or some other random set of numbers
So there is only one unique number printing? If the script is not generated at runtime you can also simply right click the script in the project folder and select "Find references in Scene" then the Scene Hierarchy will filter out all the objects that don't have the script attached.
-487432 is the one thats small projectile that working correctly
162930 this the other projectile im guessing that not popping up?
is there something wrong with my script