Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyTimer : MonoBehaviour
{
    public float currentTime = 0f;
    public float startingTime = 10f;
    public delegate void Enemyspawn();
    public static event Enemyspawn OnEnemyspawn;

    // Start is called before the first frame update
    void Start()
    {
        currentTime = startingTime;
    }

    // Update is called once per frame
    void Update()
    {
        currentTime -= 1 * Time.deltaTime;

        if (currentTime = 0)
        {
            OnEnemyspawn();
        }
    }
}


What I have tried:

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

public class EnemyManager : MonoBehaviour
{

    public Transform[] m_SpawnPoints;
    public GameObject m_EnemyPrefab;

    // Start is called before the first frame update
    void Start()
    {
        SpawnNewEnemy();
    }

    void OnEnable()
    {
        EnemyTimer.OnEnemyspawn += SpawnNewEnemy;
    }


    void SpawnNewEnemy()
    {

        int randomNumber = Mathf.RoundToInt(Random.Range(0f, m_SpawnPoints.Length - 1));

        Instantiate(m_EnemyPrefab, m_SpawnPoints[randomNumber].transform.position, Quaternion.identity);


    }

}
Posted
Updated 29-Jun-23 18:33pm
Comments
Jo_vb.net 29-Jun-23 19:09pm    
Replace
if (currentTime = 0)
with
if (currentTime == 0)

If you debug, you will see the error is being raised from:
C#
void Update()
{
    currentTime -= 1 * Time.deltaTime;

    if (currentTime = 0)
    {
        OnEnemyspawn();
    }
}

This is because you have written currentTime = 0 in the if condition.

C# supports the usual logical conditions from mathematics:
Less than: a < b
Less than or equal to: a <= b
Greater than: a > b
Greater than or equal to: a >= b
Equal to a == b
Not Equal to: a != b
Refer: C# If ... Else[^]
Thus, following would work as shared in the comment by Jo_vb.net:
C#
if (currentTime == 0)
 
Share this answer
 
In C#, an assignment uses one =, while an equality comparison uses two, ==.
 
Share this answer
 

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



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