2016-10-18 3 views
0

Что я делаю на своем уровне, таймер отсчитывает от 50 до 0, используя Time.deltaTime, это отлично работает. теперь на скрипте PlayerControl, когда я беру монету, я хочу, чтобы таймер добавлялся на 3 секунды. Мой код ниже.Добавление секунд на мой таймер

// Это PlayerControl, когда я забираю объект PlusOrb.

public class PlayerCTRL : MonoBehaviour 
{ 

    public Text timerText; 
    public TimerCount timerScript; 
    public Image DamagedOverlay; 
    public float speed; 
    public Text CountText; 
    private int count; 
    public GameObject TakeDamage; 
    public Text scoreText; 
    public TEMPLE_score scoreScript; 
    public TEMPLE_GlobalSettings globalSettings; 

    void Start() 
    { 
     CountText.text = CoinCounter.ToString(); 
     playerLife = 3; 
     count = 0; 
     SetCountText(); 
     playerAS = GetComponent<AudioSource>(); 
     timerScript = GetComponent<TimerCount>(); 
     damaged = false; 
    } 

    void OnTriggerEnter2D(Collider2D other) 
    { 
     if (other.gameObject.CompareTag("PlusOrb")) 
     { 
      Destroy(other.gameObject); 
      count = count + 1; 
      SetCountText(); 
      //score 
      scoreScript.myscore += 2; 
      scoreText.text = scoreScript.myscore.ToString(); 
      //timer 
      timerScript.timer + 3f;// this is the problem i am having 
      PlayerPrefs.SetInt("CoinCount", PlayerPrefs.GetInt("CoinCount") + 1); 
     } 
    } 
} 

// Это сценарий таймера.

public class TimerCount : MonoBehaviour 
{ 
    public Text TimeText; 
    public float timer1 = 50; 
    float SceneTimer = 0; 
    TEMPLE_GlobalSettings globalSettings; 
    public Sprite lives0; 
    public GameObject Gore; 
    public PlayerCTRL PlayerController; 
    int ouch; 


    void Start() 
    { 
     timer1 = 50; 
    } 


    void Update() 
    { 
     this.GetComponent<Text>().text = timer1.ToString("F0"); 
     timer1 -= Time.deltaTime; 

     print(timer1); 
     if (timer1 < 1) 
     { 
      timer1 = 0; 
      PlayerController.playerLife = 0; 
      SceneTimer += Time.deltaTime; 
      //if (SceneTimer > 2) 
      //{ 
      //SceneManager.LoadScene("TEMPLE"); 
      //} 
     } 
    } 

    void GameOver() 
    { 
     GameObject thisGore = Instantiate(Gore, transform.position, transform.rotation) as GameObject; 
     thisGore.GetComponent<ParticleSystem>().Play(); 
     GameObject.Find("Lives").GetComponent<Image>().sprite = lives0; 
     Destroy(gameObject); 
    } 
} 

ответ

0

Заменить

timerScript.timer + 3;// this is the problem i am having 

с

timerScript.timer += 3f; 

Или

timerScript.timer = timerScript.timer + 3f; 

Это # материал basic C. Вы должны изучить C# перед тем, как работать с Unity. Есть много учебных пособий.

EDIT:

С обновленным сценарием в вашем вопросе, не существует переменная с именем timer в вашем TimerCount сценарии. Аналогичное имя переменной в вашем сценарии TimerCount называется timer1. Вы должны либо переименовать его в timer, либо timerScript.timer1 += 3f;.

В конце дня это означает, что вам нужно изучить базовый C#. Пожалуйста, не принимайте это как оскорбление. Необходимо понять базовый C#, или вы будете задавать более похожие вопросы, подобные этому. Вы получаете доступ к переменной, не объявляется.

+0

yer Вот что я пробовал, я думал, что это сработает, но оно все еще не работает. спасибо человеку за ваш ответ. –

+0

Что не работает? Не говорите, что это не работает. Скажите нам, что именно не работает. – Programmer

+0

Я пробовал их все, и теперь его таймер отображает красную строку timerScript.timer = timerScript.timer + 3f; или timerScript.timer + = 3f; –

Смежные вопросы