So I am trying a 4th kind of shooting for my FPS, and I am having trouble making this script work. I want the bullet (a cube about 390 along the z axis) to hide when the Start () runs, and when the player clicks the left mouse button (Fire1) to show for .2 secs and then hide.
Here is the non working script:
public float showTime = 0.2f;
public GameObject Bullet;
void Start () {
Bullet.SetActive (false);
}
void Update () {
if (Input.GetButtonDown ("Fire1") {
Bullet.SetActive (true);
yield WaitForSeconds = showTime;
Bullet.SetActive (false);
}
}
jiak1
September 6, 2017, 10:41am
2
Try this:
public float showTime = 0.2f;
public GameObject Bullet;
void Start()
{
Bullet.SetActive(false);
}
void Update()
{
if (Input.GetButtonDown("Fire1") ){
StartCoroutine(doBulletThingy());
}
}
IEnumerator doBulletThingy()
{
Bullet.SetActive(true);
yield return new WaitForSeconds(showTime);
Bullet.SetActive(false);
}
modify your script like this:
public float showTime = 0.2f;
public GameObject Bullet;
void Start () {
Bullet.SetActive (false);
}
void Update () {
if (Input.GetButtonDown ("Fire1") {
StartCoroutine("showBullet");
}
}
IENumarator showBullet()
{
Bullet.SetActive (true);
yield return new WaitForSeconds = showTime;
Bullet.SetActive (false);
}
You can’t use WaitForSeconds in Update (as far as i know). that was your problem.
I trust them both equally, but which ever script works better, or which ever script works at all, they get 10 points in a reward.