Hi, I’m trying to make a simple prototype where I have a script attached to a gameObject, and that gameObject sends a raycast out in 5 different directions, up, down, left, right, and back.
I have the general idea of how this SHOULD work, and I have gotten it to work with just a single raycast, but it seems to throw an odd missing object reference exception when i make a raycast array.
public class Connector : MonoBehaviour
{
public GameObject self;
public RaycastHit[] hit;
void Start()
{
GameObject self = gameObject.GetComponent<GameObject>();
Physics.Raycast(self.transform.position, Vector3.up, out hit[0]); // This is line 12
Physics.Raycast(self.transform.position, Vector3.left, out hit[1]);
Physics.Raycast(self.transform.position, Vector3.right, out hit[2]);
Physics.Raycast(self.transform.position, Vector3.forward, out hit[3]);
Physics.Raycast(self.transform.position, Vector3.back, out hit[4]);
foreach (RaycastHit connect in hit)
{
if(connect.transform != null)
{
var joint = gameObject.AddComponent<FixedJoint>();
joint.connectedBody = connect.transform.GetComponent<Rigidbody>();
}
}
}
}
MissingComponentException: There is no ‘GameObject’ attached to the “Cube” game object, but a script is trying to access it.
You probably need to add a GameObject to the game object “Cube”. Or your script needs to check if the component is attached before using it.
Connector.Start () (at Assets/Assets/Script/Connector.cs:12)
Above ^ is the error I get when i use this code, and I tried to change self.transform.position (which I know is correct syntax) with just a transform location and a vector3, with the same error. I guess it could have something with the way i raycast, but I’m still learning so I’m not sure what Raycasting limits are.