I’m working out a system for picking up objects and placing them in slots.
I have it set up so that whenever a player is carrying an object and they click on a slot, it should make the object slotted, which is a function I’ve set up which disallows the object from moving or using gravity, as well as setting its position to the slot so I can know for sure that it’s there as a debug feature. This is all done using Physics.Raycast.
There are multiple slots around the area and any amount of the carry objects could be instantiated at runtime based on if the player presses a button, so I made it so that the position the object should be set to (essentially, the slot’s position) is the transform of the RaycastHit information so I don’t have to manually set it using drag-and-drop in the Inspector.
This is the line of code:
slotPosition.position = new Vector3(hit.transform.position.x, hit.transform.position.y, hit.transform.position.z);
The error it returns is “Object reference not set to instance of an object”.