0
\$\begingroup\$

I wanted to spawn different enemies(meteors) after a certain amount of time this is the script that I tried doing but it's giving me an error. I'm still just a beginner and I don't if I did something wrong with the script.

The error that I get is:

Assets/Scripts/meteorSpawner.cs(32,12): error CS1660: Cannot convert lambda expression' to non-delegate typebool'

and here is my current script

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

public class meteorSpawner : MonoBehaviour {

public GameObject[] meteors;
//int meteorNo;
public float maxPos = 2.4f;
public float delayTimer = 1.25f;
public float timeNeed = 10f;
public float time = 0f;
float timer;

// Use this for initialization
void Start () {
    timer = delayTimer;

}

// Update is called once per frame
void Update () {

    timer -= Time.deltaTime;
    if(timer <= 0) {
        Vector3 meteorPos = new Vector3(Random.Range(-2.2f, 2.2f), transform.position.y, transform.position.z);
        //meteorNo = Random.Range(0, 2);
        Instantiate(meteors[0], meteorPos, transform.rotation);
        timer = delayTimer;
    }
    time += Time.deltaTime;
    if(time => timeNeed){
        Vector3 meteorPos = new Vector3(Random.Range(-2.2f, 2.2f), transform.position.y, transform.position.z);
        //meteorNo = Random.Range(0, 2);
        Instantiate(meteors[1], meteorPos, transform.rotation);
    }


}
}

enter image description here

\$\endgroup\$
1
\$\begingroup\$

Your immediate problem is just a typo:

  • >= is a "greater than or equal" comparison
  • => defines a lambda expression, which is like creating a little function on the fly. The symbol is like an arrow pointing in the direction of the mapping (input) => (output)

Changing to the correct >= comparison for your use case will fix the compiler error.

But, this probably won't be what you want. Your old meteors will still continue spawning every delayTimer seconds, and your new meteors will start spawning every frame, because you keep incrementing time without resetting, so time >= timeNeed will be true for every subsequent frame.

If instead you want to change from periodic spawning of the first meteor type, to periodic spawning of the second meteor type, what you might want instead is a coroutine like this...

// Start a coroutine as soon as the object is spawned & enabled in the scene.
// This coroutine can suspend & resume itself so it continues running across
// multiple frames for the lifetime of the object.
IEnumerator Start() { 
    // For the first portion of time, spawn basic meteors.
    for(float time = 0; time < meteorChangeDelay; time += meteorSpawnDelay) {
         Spawn(meteors[0]);
         // Wait until the next spawn time.
         yield return new WaitForSeconds(meteorSpawnDelay);
    }

    // Then switch to spawning advanced meteors for all future spawns.
    while(true) {
         Spawn(meteors[1]);
         yield return new WaitForSeconds(meteorSpawnDelay);
    }
}

void Spawn(GameObject spawnable) {
    Vector3 position = transform.position;
    position.x = Random.Range(-2.2f, 2.2f);
    Instantiate(spawnable, position, transform.rotation);
}
\$\endgroup\$
  • \$\begingroup\$ Thank you so much but unfortunately I don't know how to insert the code you have given me. UPDATE:I tried delete my code and inserting yours but that only spawns the new meteors on every frame \$\endgroup\$ – DeeMastah Sep 28 '18 at 15:34
  • \$\begingroup\$ Then I recommend working through some basic Unity tutorials until you have a solid grasp of the fundamentals. This replaces your existing code, but tuning it to work exactly the way you want will require that you understand the code, not just copy and paste it. If anything is unclear, I'm happy to explain in more detail. You'll also find all of this terminology is covered extensively in tutorials, so if you're curious how "coroutines" or "WaitForSeconds" work, searching those terms will bring up lots of resources \$\endgroup\$ – DMGregory Sep 28 '18 at 15:38
  • \$\begingroup\$ Alright thanks alot! I really appreciate all your help even on my previous posts. I'll try my best to learn the basics first and then I'll tweak the code you gave me :) \$\endgroup\$ – DeeMastah Sep 28 '18 at 15:50
  • \$\begingroup\$ Is there a way for me to contact you and ask questions? I also just understood how "coroutines" and "WaitForSeconds" work but my current problem now is how do I proceed to spawning the second meteor after the for statement is done. I tried if statements but I can't seem to get it to work. \$\endgroup\$ – DeeMastah Sep 28 '18 at 17:18
  • \$\begingroup\$ The code above already proceeds to spawning the second meteor type. The for loop finishes, and the execution continues from the next line, where the while loop spawns the new meteor type from that time onward. You don't need any extra if statements at all. \$\endgroup\$ – DMGregory Sep 28 '18 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.