Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question I need help at my reload function

Discussion in 'Scripting' started by MuffinLP, Jul 31, 2020.

  1. MuffinLP

    MuffinLP

    Joined:
    Jul 1, 2020
    Posts:
    3
    Hey guys,
    I have got a problem at my reload function.
    I am trying to start a timer after the button "r" is pressed. This timer should be 5 seconds long.
    I don't get the problem.

    Here is my script.


    using System;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class shoot : MonoBehaviour
    {
    public float offset;
    public bool full = true;
    public int ammo = 30;

    public GameObject projectille;
    public Transform shotPoint;

    private float timeBtwShots;
    public float startTimeBtwShots;

    void Update()

    {
    if (full == true)
    {
    if (Input.GetMouseButtonDown(0))
    {

    Instantiate(projectille, shotPoint.position, transform.rotation);
    timeBtwShots = startTimeBtwShots;
    ammo--;
    }
    }

    if (ammo == 0)
    {
    full = false;
    }

    if (Input.GetButtonDown("reload"))
    {
    StartCoroutine(StartTimer(50));
    }
    }

    IEnumerator StartTimer(float timeLeft) //This StartTimer is the problem
    {
    Reload();
    }

    void Reload()
    {
    ammo = 30;
    }
    }
     
  2. ServantOMallard

    ServantOMallard

    Joined:
    Aug 17, 2018
    Posts:
    14
  3. MuffinLP

    MuffinLP

    Joined:
    Jul 1, 2020
    Posts:
    3
    Thanks a lot.
    I got it. Now it works
     
    ServantOMallard likes this.