Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I'm trying to create a short 2D game where you can wait x amount of time to get for example 50 "troops". This for now works fine, but now I need a script that detects the number of troops I have (at the startpoint) and send them out, just like small dots going from start point to an endpoint, but not one after another more like 5 in a row * 10.

Troops are counted upwards every second with a float, so could we use that or do I need something other for that?

What I have tried:

C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TroopsPerSecond : MonoBehaviour
{
    public Text troopText;
    public float troopAmount;
    public float troopIncreasedPerSecond;

    // Start is called before the first frame update
    void Start(){
        troopAmount = 0f;
        troopIncreasedPerSecond = 10f;
    }

    // Update is called once per frame
    void Update(){
        if (troopAmount < 50f){
            troopAmount += troopIncreasedPerSecond * Time.deltaTime;
            troopText.text = troopAmount.ToString("F0");
        }
        else{
       print("Troops full");
       }
    }
}
Posted
Updated 9-Sep-21 5:53am
v2
Comments
Richard MacCutchan 9-Sep-21 6:36am    
Unless you need actual values less than 1 then you should use integers for count values.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900