C
C#5d ago
merlo17

if statement always returns true

So I have an if statement to check if a value in another script is null or not. However it always returns true, even when I know it is not null. Since this value is necessary for what comes after that if statement when it returns false, I can't gut it, and when I do, the rest of the script fails because the value is apparently null to the script checking for the value. Screenshot 1: the if statement Screenshot 2: Where the value is and highlighted is where it is getting set
No description
No description
9 Replies
Cattywampus
Cattywampus5d ago
the it's null as the error says 1st thing to check make sure you subscribed to the said event
merlo17
merlo17OP5d ago
okay, how I know the value isn't null though, because in the same script that it's set, it has to run a process using that value, then wait a set time until it uses that same value again before clearing it So somehow, the script that I have that if statement in, isn't properly reading that value
Cattywampus
Cattywampus5d ago
I mean you can easily check if the event passed via ctor is null or not, OR make sure you don't unsub it somewhere else too a bit busy atm, so my reply might take a bit longer
merlo17
merlo17OP5d ago
np, I was just cooking Ok, so A. How do I do either of those? I've read over the code so many times and never found anything to tell me, hey this isn't going to read this. B. It can read a fixed value such as a ulong, but not a nullable value that changes Should I just do the cheap thing of merging the two scripts? The script I'm having trying to read the value is essentially only going to read from that script I'm currently trying to get it to read from
Cattywampus
Cattywampus5d ago
also you're doing something else there_eventHandler.activeEnemy so the event might not be null, BUT the activeEnemy might so make sure you check that
merlo17
merlo17OP5d ago
activeEnemy isn't null though activeEnemy gets set by the task StartRandomEncounters() in my EventHandler script Then in the other script, I have if (_eventHandler.activeEnemy == null) as a stopping point so that the program doesn't shit itself if someone tries to do something while it is null, but it's when it isn't null, it still says that it is okay, so putting them in the same script and getting rid of _eventHandler did nothing to help Either activeEnemy = SelectRandomEnemy(); is not setting Public EnemyAircraft? activeEnemy correctly or if (activeEnemy == null) is not properly reading Public EnemyAircraft? activeEnemy one of the two and I don't know which, it's all in the same script now
Thalnos
Thalnos5d ago
the runtime doesn't lie, when if(_eventHandler.activeEnemy == null) resolves true then you can trust that it IS null show us how you call EngageCommand() and how you call StartRandomEncounters() oh never mind, not needed. I thought StartRandomEncounters() may not get invoked at all, but you're simply setting it to null at the end so that will be why its null. The method ends with activeEnemy = null;
merlo17
merlo17OP5d ago
await Task.Delay stops it from getting to that early. So the issue is that I have two instances of my EventHandler script _eventHandler.activeEnemy is trying to pull from the first, while activeEnemy is being set in the second So my question is, how do I prevent multiple instances
merlo17
merlo17OP5d ago
You can ignore the log about EventCmdModule
No description

Did you find this page helpful?