#Code is not working, I have been debugging this shit for 2 hours now with chat GPT.

1 messages · Page 1 of 1 (latest)

slate mica
#
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
using UnityEngine.Tilemaps;

public class StartEvent : MonoBehaviour
{

    public Tilemap tilemap;
    public GameObject[] spawners;
    public float enemyCount;
    public bool hasStartedEvent = false;
    public GameObject border;
    public TilemapCollider2D c;

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


        TurnOnSpawners(false);
    }

    // Update is called once per frame
    void Update()
    {
        Debug.Log("enemyCount: " + enemyCount);
        Debug.Log("hasStartedEvent: " + hasStartedEvent);
        if (hasStartedEvent == true)
        {
            Event();

        }

        if (enemyCount == 3)
        {
            Debug.Log("Stopping event because enemy count is " + enemyCount);
            enemyCount = 0;
            StopEvent();

        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.layer != 8)
            return;


        Debug.Log("EnterTrigger");

        hasStartedEvent = true;

        Destroy(tilemap);
    }

    public void Event()
    {
        border.SetActive(true);
        TurnOnSpawners(true);
    }

    public void StopEvent()
    {
        hasStartedEvent = false;
        TurnOnSpawners(false);
        border.SetActive(false);
    }

    public void TurnOnSpawners(bool s)
    {
        for (int i = 0; i <= spawners.Length; i++)
        {
            spawners[i].SetActive(s);
        }
    }
}

the ``` if (enemyCount == 3)
{
Debug.Log("Stopping event because enemy count is " + enemyCount);
enemyCount = 0;
StopEvent();

    }``` is not being run even though the condition IS Being met. I tried everything
#

I have no fucking clue what to do

topaz wharf
#

Does the event ever start in the first place?
Also why is enemyCount a float? Logically, counts usually are ints (or other integral number formats). Not to mention that comparing a float to another number is not always reliable due to how floating point numbers work.

#

And, unrelated to functionality, but why is your method named "TurnOn..." when it takes a bool that also allows turning off? "ToggleSpawners" may be a better terminology here. Don't write code that may end up confusing yourself.

chilly oar
#

lol I'm not sure chatgpt was helping or hurting

dusk crag
#

Can you try this, just to Debug:

    if (enemyCount == 3)
    {
        Debug.Log("Stopping event because enemy count is " + enemyCount);
        enemyCount = 0;
        StopEvent();

    }
**else {
       Debug.Log("Enemy count is " + enemyCount);
    }**
}
livid mauve
#

where is enemyCount being incremented?

topaz wharf
#

it's public, could be anywhere

chilly oar
#

Imagine it's a property and the getter just screws with the value

slate mica
slate mica
slate mica
slate mica
#

hell, im not even sure how the event itself will function, im just testing. This game was uploaded for the Brackeys Game Jam 2023.1, the game uploaded is shit, but the thing that you do is basically what the events will be. People did find the game fun, so I wanted to make sure it IS still fun.

#

I think i will just

#

copy the script

#

delete the script file

#

make another script file

#

paste the script

slate mica
#

but first imma just remake the code because might aswell

slate mica
#

I am so confused on why this is not working

dusk crag
#

It's only being called once? Is the script being destroyed or disabled somehow?

wide lantern
#

may be try enemyCount == 3f

slate mica
#

it didnt work

#

shitt

slate mica
tepid hatch
#

why is enemyCount a float

#

You're doing == on floats, which is not going to give you the results you want

#

Also == true is redundant

slate mica
tepid hatch
pulsar sphinx
#

You should post the code that is incrementing enemy, this may be where the issue lies

tepid hatch
#

I just.. don't understand that reasoning

#

"I prefer using floats"

#

It's not about what you prefer. It's about using what is right for the job

#

You could prefer eating soup with a fork. That doesn't mean your choice is a smart one

slate mica
slate mica
#

it is being incremented properly

#

I debugged it already

tepid hatch
#

Case in point run this code:

Debug.Log(0.1 + 0.2 == 0.3);

You as a human know this is true. However this will log False

slate mica
#

interesting

#

I guess, I know not to do that now

#

this is the second stupid shit I have done recently

tepid hatch
slate mica
#

like 20min ago I crashed into some dudes car with my bike while sliding..

#

I pulled the break lever

#

but just kept sliding

#

but anyways

#

I fixed it tho code still doesnt work

pulsar sphinx
# slate mica its not

Why not post it anyway? I don't see anywhere in the code you posed that debugs the enemies count except for the block you said is not exectuting

#

if that block isn't executing it means something is more than likely wrong with how enemyCount is being incremented

slate mica
#

but usre

#

sure

#

I can

pulsar sphinx
#

my bad, missed that

lucid sequoia
#

Okay just to summarize, what's the problem, are there any errors, what were you trying to accomplish

dusk crag
#

Here's the thing. If that Debug statement at the top of Update() isn't being called every frame, the script isn't running. The only explanation is that the script has been disabled or destroyed.

slate mica
#

this code is not being run

#

the condition IS being met

tepid hatch
#

Is it logging that message to the console?

slate mica
#

the "Stopping event...

#

yes

#

well not

#

no

#

it should

#

but it is not

tepid hatch
#

Then the condition is not being met

slate mica
#

but thats impossible

#

the enemy count does reach 3

#

I debug.logged it

#

and it shows up as 3 or above in the editor

#

this has no reason not to work

tepid hatch
#

Okay but this code doesn't care if it's above

#

It's checking if it's equal to 3

slate mica
#

still

tepid hatch
#

No not still, that is a huge difference

slate mica
#

it doesnt do anything even if it is 3

#

plus

#

originally

#

it was

#

=

#

and it didnt work then either

#

I can change it to be safe ig

#

im very close to saying "Unity's fault" and abandoning the entire idea

#

god if it turns out to be something stupid

tepid hatch
#

Idk what you want me to tell you. The condition is clearly evaluating to false. If enemyCount is still a float, like I suggested it not be, that can be a reason.
But also, not a single line of code in this entire class you sent modifies enemyCount except for setting it to 0. How it is going to reach 3, when nothing changes it to 3?

slate mica
#

it does reach, it chages in another script

#

not a float anymore

#

and weirdly enough

#

some stuff happens when I manually

#

set

#

hasStartedEvent

#

to false

#

I am not sure why, but I doubt it has anything to do with this

lucid sequoia
#

First I don't see code here that's setting enemyCount anywhere, which is mildly concerning
Secondly you likely have an additional instance, write the following to confirm, if you get more than one type of hash, that's why

Debug.Log($"Hash value:{this.GetHashCode()}");

Alternatively replace GetHashCode with GetInstanceID for Unity specific Identifier

lucid sequoia
#

It should not be

slate mica
#

it should

#

there is a reason for it

lucid sequoia
#

Fourth you should use >= for sanity checking not ==
No it should not

slate mica
#

how am I supposed to increase the ammount of enemies kiled (yes, my slow ass named the kill count "Enemy Count")

#

in the script

slate mica
#

I changed it to == in desperation tho I changed it back to >= now

lucid sequoia
#

SOLID principles, nothing should mutate a classes state but itself unless absolutely necessary, the above is the reason why

You could use any of the following to increment it:
Observer Pattern
Singleton Pattern
Command Pattern

#

A simple event would suffice, or have the spawner just keep track of how many objects it's spawned versus how many have been destroyed

pulsar sphinx
#

You should definitely post the code that is incrementing enemy count

slate mica
#

alr

#

for more dirty code

#

be prepared for more dirty code*

#

I got to fix that eventually

#


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

public class MinotaurDie : MonoBehaviour
{
    public float hp;
    [SerializeField] EnemiesKilledCount code;
    [SerializeField] Event s;
    public GameObject g;
    // Start is called before the first frame update
    void Start()
    {
        code = FindObjectOfType<EnemiesKilledCount>();
        s = FindObjectOfType<Event>();
    }

    // Update is called once per frame
    void Update()
    {
        if (hp <= 0)
        {
            if(s.hasStartedEvent == true)
            {
                s.enemyCount++;
            }
            Destroy(gameObject);
            code.killCount++;
            Instantiate(g, transform.position, transform.rotation);
        }
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.layer == 7)
        {
            hp -= 25f;
            FindObjectOfType<ArrowCount>().arrowInc++;
        }
    }
}
#

I will fix it later

lucid sequoia
#

There's a lot here lmfao

slate mica
#
        if (hp <= 0)
        {
            if(s.hasStartedEvent == true)
            {
                s.enemyCount++;
            }
            Destroy(gameObject);
            code.killCount++;
            Instantiate(g, transform.position, transform.rotation);
        }
    }```
#

this is

#

the relevant part

lucid sequoia
#
if(s.hasStartedEvent == true)
            {
                s.enemyCount++;
            }
            Destroy(gameObject);
            code.killCount++;
            Instantiate(g, transform.position, transform.rotation);

The destroy here is really bad, the object is technically destroyed before calling code.killCount and Instantiate, if this wasn't Unity you'd never execute this code

slate mica
#

tbh, the event system code will get replaced most likely

slate mica
#

I checked

lucid sequoia
#

That's not the problem, you're destroying the object and still using it

slate mica
#

elaborate

lucid sequoia
#

It's like firing an employee and telling them to finish their shift

#

Destroy is delayed by 1 frame for this exact case, had you used a language like C++ and done this it wouldve never executed the code because the object wouldve been destroyed and freed before executing this

#
code = FindObjectOfType<EnemiesKilledCount>();
        s = FindObjectOfType<Event>();

I won't go into detail but avoid Find methods at all costs, event UnityEvents are better or the SingletonPattern as much as I hate it

slate mica
#

yes yes

#

ik

#

but I truly didnt care at the time

lucid sequoia
#
 if(s.hasStartedEvent == true)
            {
                s.enemyCount++;
            }

This may never run if hasStartedEvent is false on the instance you're referencing meaning enemy count would never be incremented

slate mica
#

yes

#

thats the point

lucid sequoia
#

It would be, but if you have a race condition, which is entirely possible, the code won't execute in the order you think

slate mica
#

still

#

it does work for whatever reason

lucid sequoia
#

Andddd this loops back to the Single Responsibility principle I was mentioning earlier

#

Minotaur should not be ensuring that the spawner does things, the minotaur should simply announce it's death

#

the spawner can then listen to that event and increment accordingly

lucid sequoia
slate mica
#

s actually stood for start event, thats how the script was called at the time.

#

aka the main script the first one I sent

slate mica
lucid sequoia
#

Unity does a lot behind the scenes, without understanding it it's very easy to make mistakes that would break your code, but even when you fix it you won't understand why it broke

slate mica
#

interesting

lucid sequoia
#

Take the race condition I mentioned, normally this wouldn't be an issue

#

However unity has an order that scripts are run, script execution order, in a setting where class A dies and needs to notify class B what would happen if that order changed when you built your game and now class B is running before class A?

#

Technically not a real race condition but close enough for synchronously running code

slate mica
#

interesting

#

got any suggestions, since it seems Unity is boycotting my code workin

#

i literally cant think of anything productive to do with the game

#

my only idea now is

#

Remake it all

#

the entire Event system

#

you know what

#

I will

#

this is a waste of my time