Accessed None When Getting Object Reference

Hi,

I asked this a few days ago, but received no response, so figured I’d ask again using a slightly more appropriate heading.

Mechanically, my scene involves a security camera (basically a spotlight component) with a collision box attached to it. If the player walks into the collision box, the camera immediately stops its rotation pattern and tracks the player until the player leaves the collision box; then the camera returns to rotation. The idea is, lights in the environment will turn on and rotate when the trigger is activated by the player in the SecurityCamera class giving the visualisation of an alarm.

I’m essentially attempting to set the boolean “SecurityCameraDetectedPlayer?” to false on EventBeginPlay and toggle lights to OFF, until BeginEventOverlap is triggered in the SecurityCamera class. That will then enable the lights to turn on in the EnvironmentLight class, but only once it’s set to True. This is class-to-class and doesn’t involve the level blueprint.

The issue basically involves me being unable to set the boolean “SecurityCameraDetectedPlayer?” in my “EnvironmentLight” class due to: “Accessed None trying to read property SecurityCameraTargetInstance”. Blueprint: EnvironmentLight Function: Execute Ubergraph Environment Light Graph: EventGraph Node: Set SecurityCameraDetectedPlayer?

Now, I initially attempted casting to SecurityCamera, but couldn’t get the object reference. I then tried getting all actors with the array and that didn’t function. Finally, after wandering around the net and looking at tutorials, I created a variable and made it an object reference of the same class, then attempted to set it within the EnvironmentalLight class. All of these are either “NOTE - not necessary to cast”, or “accessed none.” The latter being accessed none. I’m aware of the fact the variable is empty, and that casting is a common problem in OOP; I’ve been frantically looking around these last few days and learned a bit. But I’m also not as experienced as I’d like to be and this can be confusing.

Attachment 1 is the security camera BP showing the logic. The 2nd is the light. They show setting the same boolean I’ve used as a condition to check if the player has been detected.

Anyone able to assist, please? Thanks! If this isn’t 100% clear, please say so. And any other workarounds would be useful to know. Thanks again.

You are setting Security Camera Target Instance to itself. Which means if it is null you are setting the Var to null. You need to get a valid referenced from the game world where it exists or store a reference after spawning it. If it exists in the world Get All Actors from class, specifying the class and get the first index in the array assuming you only have one should get you a valid reference.

If you do get a valid reference to it shows us how you are getting it and setting it.

Check this out and see if it helps.

Thanks for the reply.

Without wishing to sound completely confused, what ways do you ascertain would be useful for getting a valid reference from the world, or storing the reference? Perhaps everyone’s project is slightly different, but as far as I can tell, the reference seems unattainable currently.

While I wouldn’t say I’m completely new to this, I’d appreciate a detailed answer, if possible. That way I can figure out where I’ve gone wrong and hopefully not repeat the mistake.

Thanks!

Thanks again for the reply.

Based on what you said, I went in and realised I was probably being silly. Essentially, I was initially attempting to cast when I shouldn’t have been. Earlier I was following some bad advice about apparently having to cast to another class to receive the set variable I needed for SecurityCameraDetectedPlayer? Obviously that wasn’t needed. After I disregarded the empty reference which I created to simply get any reference and just grabbed all actors of class, the lights in my environment function when the camera detects the player as intended.

I changed a couple of things around, though, as seen in both attachments. It’s not completely perfect, as the get all actors of class method doesn’t currently seem to allow multiple instances of the EnvironmentalLight class from lighting up, but I’ll get there.

Thanks for the advice.

That looks better! Yeah if you want to get them all to light up and not just one aka the first element in the array of get all actors of class then you need to add a For Each node that loops through all the elements in the array and move the execution from the single get (0) to the current element in the array that the for each loop will iterate through.

For anyone else with similar issues about casting, references and accessed none errors you can check out video #1, 25 and 27 respectively in the link below. It covers the basics of these topics.