- Home /
Enemy spawn logic not working
I am making a top down 2D game. I have 12 predefined spawn points for enemies, represented by a sprite on screen. on this sprite I have the following script.
using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine;
public class LogicSpawn : MonoBehaviour
{
//RNG
System.Random RNG = new System.Random();
public string Direction;
public Transform DragonPrefabGreen;
public Transform DragonPrefabGold;
//update once per frame
void update()
{
//conects this script to the drgon tracker script
DragonTracker dt = GameObject.Find("_DragonManager").GetComponent<DragonTracker>();
Timer T2 = GameObject.Find("TimerText").GetComponent<Timer>();
//spawn logic variables
int RSpawn = RNG.Next(0, 2);
print(RSpawn);
print("RSpawn");
int DragonType = RNG.Next(0, 101);
bool GoldDragonInit = dt.GoldDragonInit;
int DragonCount = dt.DragonCount;
int Difficulty = dt.Difficulty;
//spawning logic
if ((RSpawn == 1) && (DragonCount < Difficulty))
{
if (DragonType > 99)
{
//summon regular dragon
Instantiate(DragonPrefabGreen, transform.position, transform.rotation);
}
if ((DragonType == 100)&&(GoldDragonInit==false))
{
//Sumon gold dragon
Instantiate(DragonPrefabGold, transform.position, transform.rotation);
}
}
}
}
I have a parent game object that all of these sprits are placed beneath in the hierarchy. this game object has a script that contains all of the spawning variables. This keeps track of the current number of enemies in the game, if a special enemy is in play, and to set the difficulty level.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DragonTracker : MonoBehaviour
{
// is gold dragon in play?
public bool GoldDragonInit = false;
// curently active dragons
public int DragonCount = 0;
// defalts to 5
public int Difficulty = 5;
}
the problem I am having is the spawning does not appear to work. the only thing I can think of is the random number generator is not working, which Is why I put the print (RSpawn);. nothing appears in the console so it appears that this may be the case but I don't know why it doesn't work.
any help is greatly appreciated.
I will also need to change the variables in the variable tracking script when different events happen, such as an enemy spawns. if anyone could tell me how to do that while I am here that would be great.
thanks is advanced.
Answer by aldonaletto · Apr 19, 2019 at 05:00 PM
The function Update is misspelled as update. But you should also improve your code, moving part of it to Start:
public class LogicSpawn : MonoBehaviour
{
//RNG
System.Random RNG = new System.Random();
public string Direction;
public Transform DragonPrefabGreen;
public Transform DragonPrefabGold;
// move these variables outside any function
DragonTracker dt;
Timer T2;
void Start(){
//connects this script to the dragon tracker script
dt = GameObject.Find("_DragonManager").GetComponent<DragonTracker>();
T2 = GameObject.Find("TimerText").GetComponent<Timer>();
}
void Update(){
//spawn logic variables
int RSpawn = RNG.Next(0, 2);
print(RSpawn);
print("RSpawn");
int DragonType = RNG.Next(0, 101);
bool GoldDragonInit = dt.GoldDragonInit;
int DragonCount = dt.DragonCount;
int Difficulty = dt.Difficulty;
//spawning logic
if ((RSpawn == 1) && (DragonCount < Difficulty))
{
if (DragonType > 99)
{
//summon regular dragon
Instantiate(DragonPrefabGreen, transform.position, transform.rotation);
}
if ((DragonType == 100)&&(GoldDragonInit==false))
{
//Sumon gold dragon
Instantiate(DragonPrefabGold, transform.position, transform.rotation);
}
}
}
}
Your answer

Follow this Question
Related Questions
How would 1 instantiate a cube on a random position on screen , ortographic camera(2d game)? 1 Answer
Multiple spawn location with random game object in random time spawn c# 1 Answer
Spawn enemies so they aren't spawned on top of each other (C#) 1 Answer
[C#] How do I randomly pick between 3 specific numbers? 1 Answer
Problem with instantiating the object in untiy at certain time intervals 1 Answer