-- enemy on deystroy for spawn limit please help

  • public int actualEntitys;
  • public int maxEntitys;
    • void Update()
  • {
  • if(actualEntitys < maxEntitys)
  • {
  • actualEntitys++;
  • Instantiate(spawnee, transform.position, transform.rotation);
  • }
  • }

so this is a spawn limit with counter how do i reference the counter from another script somone help me please this is the last thing i need in my game

Hi, first and foremost: please use code tags. It’s the first sticky on this subforum.
The easiest way to get a reference to your counter in another script, would be to make it static. Simply change it to:

public static int actualEntitys;

Now you can reference it from outside by writing ScriptName.actualEntitys.

thank you so muc

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

public class Health : MonoBehaviour
{

int MaxHealth = 100;
int CurHealth = 100;
int MinHealth = 0;

void Start()
{

}

void Update()
{
if (CurHealth < 1)
Destroy(gameObject);spawner.enemiyCounter–;
}

void OnTriggerEnter(Collider c)
{
int DamageAmount = 30;

if (c.GetComponent().tag == “RifleBullet”)
{
CurHealth -= DamageAmount;
}
}
}

did that and called it here and there are just tons of enemies spawning not sure what im doing wrong but its frusturating

Also when you are spawning too many enemies, it would be helpful to see the part where you spawn them, not the part where you calculate damage and kill them. I’ll not reply if your next post does not use code tags tho…

so first of, use please code tags as Yoreki said before,

for your problem,

if your script is spawning tons of “Entitys” it is becouse you did not set any “MAX entitys to spawn”, so as there is no “limit” it just keeps spawning,

also your code is wrong, if i can follow you right, you want to set “actualEntitys–;” not the enemyCounter, if you never set it to less, it will not spawn any entitys after reaching the goal,

i tryed to follow your code , hope it helps:

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

public class Health : MonoBehaviour
{

    int MaxHealth = 100;
    int CurHealth = 100;
    int MinHealth = 0;

    void Start()
    {

    }


    void Update()
    {
        if (CurHealth < 1)
        {
            Destroy(gameObject);
            //spawner.enemiyCounter--;
            actualEntitys--;
        }
    }

    void OnTriggerEnter(Collider c)
    {
        int DamageAmount = 30;

        if (c.GetComponent<Collider>().tag == "RifleBullet")
        {
            CurHealth -= DamageAmount;
        }
    }
}
}
public static int actualEntitys;
public int maxEntitys = 10;

void Update()
{
    if (actualEntitys < maxEntitys)
    {
        actualEntitys++;
        Instantiate(spawnee, transform.position, transform.rotation);
    }

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

public class Health : MonoBehaviour
{

int MaxHealth = 100;
int CurHealth = 100;
int MinHealth = 0;

void Start()
{

}

void Update()
{
if (CurHealth < 1)
Destroy(gameObject);spawner.enemiyCounter–;
}

void OnTriggerEnter(Collider c)
{
int DamageAmount = 30;

if (c.GetComponent().tag == “RifleBullet”)
{
CurHealth -= DamageAmount;
}
}
}

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

public class spawner : MonoBehaviour
{

public GameObject spawnee;
public float spawnTime;
public float spawnDelay;
int maxEnemies = 5;
public static int enemiyCounter = 0;
// Use this for initialization
void Start()
{
InvokeRepeating(“SpawnObject”, spawnTime, spawnDelay);
}

public void SpawnObject()
{
if(enemiyCounter < maxEnemies){
Instantiate(spawnee, transform.position, transform.rotation);enemiyCounter++;
}
}
}

it wont work just spawning ton of them

i changed the other script to

By now i have to ask: Is this a joke? Are you intentionally not doing what we told you?
On top of not using code tags (again: first sticky on this sub-forum) as you have been told 3 times now, or in every single post so far, now you also start quadruple posting?
We have an edit function. If you need to add something to your post, then edit them as long as nobody answered yet.

And to be honest, i hope nobody gives further advice until you use code tags and a bit of common sense.
Help us helping you.

1 Like

dont know what that is im new idk why your acting like that i hope you go somewhere else and never give me advise again you didnt even help me just leave please this is not a joke what makes you think i would post this and be joking who tf has time for that

like what is code flags even not everyone knows everything about this

Sorry, but this is ridiculous. You didnt know because you are new? That’s fine. That’s why i literally told you in every post where to have a look at how to use code tags. I mean, just compare the readability of code with code tags (example: Terraya) and code without code tags. It’s just really hard and slow to read without syntax highlighting, especially for longer examples.
One other thing that makes a thread hard to follow is when somebody posted multiple times between answers. I already told you about that as well, yet you did it again. New or not, it just feels like you are not reading what people tell you. Or will you claim you did not find the “Edit” button i was talking about? Again, this is ridiculous.

And sure thing. I did never help you. May wanna have a look at the first post, which answered your original question. I know it may be hard to find, since over the course of 3 replies you posted 10+ times, but it’s still there.
All i wanted from you is to use code tags so we have an easier time helping you. If you did not know what that it, or where to look for the sticky post, then ask. Dont just ignore it. The same goes for double-posting. If you just ignore what people want from you, then how can you still expect help? It’s also not as if these are some made up rules, double-posting is generally against the rules on any forum. And making it easy for people you want help from should be common sense. People helping you on this forum are doing that for free. It’s not our job.
Last but not least, it does not really help your case when you resort to name calling in the same sentence you say “sorry”.

Then again i’d like to further help you, but your code is still very unreadable. Since you now figured out how to use code tags, how about actually applying them to your code :)? This is a very easy question, so it could be done with the next answer. Or it could stretch out over 10 more posts if you keep complaining about very simple readability requests, depends on you.

got it on my own dont need your help

Glad we were able to help you. Feel free to rely on us in the future as well :slight_smile:

1 Like