I have a bullet prefab which I instantiate whenever the gun fires. The Bullet-Prefab has a script, which moves the bullet, and also a timer, which dictates for how long the bullet should travel. I do a raycast and pass the float how long the bullet should move to the gameobject. But the problem is, that The time it takes from instantiating the bullet, to bullet.GetComponent().startCount(time);
is long, and the result is the bullet penetrating the wall.
I would like to know if there is a way to instantiate the prefab with a parameter (in this case, the time) so there is no time delay between the instantiation and the passing the parameter.
This is how I move the bullet:
using UnityEngine;
using System.Collections;
public class bullet : MonoBehaviour {
public float speed = 80;
private bool stop = false;
// Update is called once per frame
void Update () {
if (!stop)
transform.Translate (Vector3.forward * speed * Time.deltaTime);
}
public void startCount(float lifeTime){
StartCoroutine(count(lifeTime));
}
IEnumerator count(float lifeTime){
yield return new WaitForSeconds(lifeTime);
stop = true;
}
}