Cannot Access Rigidbody2D on Instantiated Prefab

I cannot access any rigidbody2d on my instantiated prefab (vehicle).

When I spawn my vehicle, it appears and moves just as it should.

The prefab contains VehicleController.cs which is not only responsble for movement but for also assembling elements such as joints and rigid bodies. Here’s the excerpt which adds the rigid bodies to the wheels(only relevant code shown):

public class VehicleController : MonoBehaviour {
	
	public float speed;
	public float aceleration = 0.15f; // Vehicle acceleration amount.
	public float breakForce = 0.1f; // Amount of vehicle braking force.
	
	public GameObject[] wheels = new GameObject[2]; // Array with all the wheels from the vehicle.
	public Wheel_Options wheelOptions; // Parameters for the WheelJoint2D.
	public LayerMask whatIsGround; // LayerMask that contain ground layers.
	//private GameObject whatIsGround;
	private bool grounded; // Show if the vehicle is on the ground.
	
	public int coinsCollected;
	public AudioClip coinCollectSound;

	private bool accel;
	
	void Start () {

		DataManager.control.Load (); 
	
		coinsCollected = DataManager.control.coins;
		
		speed = DataManager.control.engine;

		for (int i = 0; i < wheels.Length; i++) {
			// Verify if all the wheels are assigned
			if(wheels *== null){*
  •  		wheels = new GameObject[0];*
    
  •  		Debug.Log("Wheel not assigned.");*
    
  •  		return;*
    
  •  	}*
    
  •  	// Verify if all the wheels has a CircleCollider2D*
    

_ else if (wheels*.GetComponent() == null){_
_
wheels = new GameObject[0];_
_
Debug.Log(“Circle collider no assigned to the wheel.”);_
_
return;_
_
}else{_
_ //wheels.GetComponent().sharedMaterial = groundMat;
addWheelJoint2D(i);
}
}*_

* }*

* // Add a WheelJoint2D to the wheel contained in the array in a specific position*
* public void addWheelJoint2D(int wheelPosition){*

* // Add the WheelJoint2D component to the wheel*
* WheelJoint2D wheelJoint = gameObject.AddComponent(“WheelJoint2D”) as WheelJoint2D;*
* // Verify if the wheel has a rigidbody2D.*
* if(!wheels[wheelPosition].rigidbody2D)*
* // Add a rigidbody2D to the wheel.*
* wheels[wheelPosition].AddComponent(“Rigidbody2D”);*

* // Set the rigidbody2D of the car to the wheelJoint2D*
* wheelJoint.connectedBody = wheels[wheelPosition].rigidbody2D;*
* // Create a new JointSuspension2D and set the variables to get a cool and real suspension*
* JointSuspension2D suspension = new JointSuspension2D();*
… // Removed for readability
* wheels [wheelPosition].GetComponent ().gravityScale = wheelOptions.gravityScale;*
* }*

… // Removed for readability. Verified that script does successfully add rigidBody2D
}

VehicleSpawner.cs:
public class VehicleSpawner : MonoBehaviour {

* private GameObject selectedVehicle;*

* // Use this for initialization*
* void Start () {*

* DataManager.control.Load ();*

* selectedVehicle = DataManager.control.selectedVehicle;*
* Instantiate(selectedVehicle,new Vector3(-20,2,0),Quaternion.identity);*

* Debug.Log (selectedVehicle.rigidbody2D);*
* Debug.Log (selectedVehicle.GetComponent().wheels[1].rigidbody2D);*
* // Both debug statements return null*

* }*

}
As noted by the two debug statements, any attempt to access rigidbody2D returns null.
I need access to the rigid bodies for my CameraFollow script:
public class CameraFollow : MonoBehaviour {

* private Transform target;*
* public Vector3 offset;*
* public float minZoom = 8.0f;*
* public float maxZoom = 12.0f;*

* void Awake (){*

* camera.orthographicSize = minZoom;*

* }*

* void Start (){*

* DataManager.control.Load ();*

* }*

* void FixedUpdate () {*

* target = DataManager.control.selectedVehicle.transform;*

* Vector3 pre;*
* pre = target.transform.position;*
* pre = new Vector3(pre.x,pre.y,-10);*
* transform.position = pre + offset;*

* camera.orthographicSize = Mathf.Lerp(camera.orthographicSize, 5 + Mathf.Abs(target.rigidbody2D.velocity.x / 2), Time.deltaTime);*

* }*
}

One issue: You’re instantiating “selectedVehicle”, but then calling .rigidbody2D on the prefab, not the instantiated object. The way to fix this is to assign the result of Instantiate to a variable:

selectedVehicle = DataManager.control.selectedVehicle;
GameObject newVehicle = (GameObject)Instantiate(selectedVehicle,new Vector3(-20,2,0),Quaternion.identity);
Debug.Log (newVehicle.rigidbody2D);

The script you show only mentions adding rigidbodies to the wheels, not the vehicle itself, and I can’t tell if/when that method is being called, so I’m afraid I can’t offer much help on that.

I didn’t see an option to mark a comment as an answer. So based on @cascaid’s input here’s the answer.

I’m still using my VehicleSpawn script and I’ve placed that script onto a an empty game object. I’ve added a public field for the camera within my VehicleSpawn script. The camera is of course accessed by dragging the camera object into that public field.

At this time, accessing a rigidbody of an instantiated prefab is easiest when done within the same script in which the prefab is instantiated.

VehicleSpawner:

public class VehicleSpawner : MonoBehaviour {
	
	private GameObject selectedVehicle;
	private GameObject vehicle;
	
	public Camera myCam;
	
	public Vector3 offset;
	public float minZoom = 8.0f;
	public float maxZoom = 12.0f;
	
	void Awake (){
		
		myCam.orthographicSize = minZoom;
	}
	
	void Start (){
		// Spawn vehicle
		DataManager.control.Load ();
		selectedVehicle = DataManager.control.selectedVehicle;
		vehicle = (GameObject)Instantiate(selectedVehicle,new Vector3(-20,2,0),Quaternion.identity);
		
	}
	
	void FixedUpdate () {
		
	
		// Access velocity of rigidbody for camera use
		myCam.orthographicSize = Mathf.Lerp(myCam.orthographicSize, 5 + Mathf.Abs(vehicle.rigidbody2D.velocity.x / 2), Time.deltaTime);
		

		
	}
}